aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why object defined in the method are created in the heap memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why object defined in the method are created in the heap memory?" Watch "why object defined in the method are created in the heap memory?" New topic
Author

why object defined in the method are created in the heap memory?

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580




As i understand , the Object aa defined in the method is created in the heap memory and the reference of it is stored in the stack memory. Why is it created in the heap memory and why it is not created in the stack memory itself. Because, as the method returns the reference will removed , which makes the object ready for GCed. So i was thinking why not it be created in the stack memory at the first place.


And where are the object 'a' and its reference created? stack or heap.
Basically what is the logic that defines that the particular object should be defined in the stack or heap ?

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Objects are always stored on the heap.


Mohamed Sanaulla | My Blog
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I suggest you to go through the JVM Specification for these kind of JVM architecture questions.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Let's assume for a moment that objects may indeed be allocated on the stack:
Now, myObj would be on the stack. And it will get deleted again once doSomething() returns. What should doSomeMore() do to preserve the value so it can be printed in the main method?
The only way this can happen is if enough space is on the heap *somewhere* that can hold obj (and we don't even know how much space this has to be, since the Object passed to doSomeMore could just as well be a String or any other subclass of Object); and we copy the contents of obj into object, whenever an assignment is made.

Let's say we could magically predict how much space has to be available (through a size variable in Object?), wouldn't you agree that having to copy the entire value of an object is extremely wasteful, compared to just copying a reference? And you *have* to copy the value, because the value ceases to exist once the method is popped from the stack.
Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75



Good example. So only references to the instances are removed from stack when methods return, but instances can be "re-used" because there are still stored in heap.

OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why object defined in the method are created in the heap memory?