Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an object be stored on the stack instead of the heap in java?

Tags:

java

Can an object be stored on the stack instead of the heap?

I recently gone through this blog http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/

Can an object be stored on the stack instead of the heap?

Yes, an object can be stored on the stack. If you create an object inside a function without using the “new” operator then this will create and store the object on the stack, and not on the heap. Suppose we have a C++ class called Member, for which we want to create an object. We also have a function called somefunction( ). Here is what the code would look like:

they suggesting that Objects can stored in Heap. and it was confusing.

I thought that,

  1. All objects in Java are stored on the heap.
    whether it is created by
    a. new Keyword
    b. Using Class.forName().
    c. Using clone().
    d. Using newInstance() method
    e. Using Object Deserialization.

  2. Methods,threads and variables are in stack.

  3. Class variables(Static variables) are stored as part of the Class object associated with that class. This Class object can only be created by JVM and is stored in permanent generation.

Please do correct me if i am wrong.

Now my doubt is whether objects can reside or stored in stack in any form.

Thank you.

like image 206
user_vs Avatar asked Sep 10 '15 11:09

user_vs


People also ask

Are objects stored in stack or heap Java?

Java Heap Space is used throughout the application, but Stack is only used for the method — or methods — currently running. The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application.

Can an object be on the stack?

Unlike Java, instances of classes ("objects") can be allocated on the stack in C++. That is, you do not need to use new to allocate an instance of an object.

Why objects are not stored on stack?

If you allocate an object on the stack, its life time is limited to the current method call. When the method returns, the object is automatically destroyed, together with everything else stored in the stack frame.

Do objects reside in stack memory?

Are they in Stack memory? No, they are in a different memory called “Heap Memory” (also called the Heap). To store objects, we need memory with dynamic memory allocation (i.e., size of memory and objects can change).

Where are variables stored on the heap?

What that value points to can be stored anywhere (heap, stack, global). Anything is possible. As for the local variables within methods of an object, they are stored on the stack, not within the objects contiguous space on the heap. The stack is usually created of a fixed size at runtime.

What is stack and heap memory in Java?

In the above example from JournalDev.com, the use of Stack and Heap is explained as follows: All Runtime classes are loaded into the Heap Space when the program is run. Java Runtime creates Stack memory to be used by main () method thread when it is found at line 1.

How to create an object in heap in Java?

The statement new Account () will create an object of account in heap. The reference variable “ref” will be created in a stack java. The assignment “=” operator will make a reference variable to point to the object in the Heap. Once the method has completed its execution.

Should objects be stored on stack or heap in C++?

Neither the C nor C++ language standards specify whether something should be stored on a stack or a heap. They only define object lifetimes, visibility, and modifiability; it's up to the implementation to map those requirements to a particular platform's memory layout.


1 Answers

The comment you have quoted refers to C++, which can store objects on the stack. In Java, you cannot.

like image 88
rghome Avatar answered Sep 28 '22 14:09

rghome