aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Store Local Variable In Instance Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Store Local Variable In Instance Variable" Watch "Store Local Variable In Instance Variable" New topic
Author

Store Local Variable In Instance Variable

Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Hi guys,

As i read instance variables and objects are stored in heap.
Local variables (declared inside a method) are stored in a stack.
once the method completes its execution the stack is cleared from the memory.

see this code:



executing the above code i will get "[22,77,99]" ... but my questions is:

is local variable and stored in the stack.
when i invoke what exactly happens, the object itself is copied or its contents?
the variable "Integer in" will be eligible for garbage collection after the method execution finishes or not (it should be, since once the method is completed, all its stack belongings are thrown away, isn't it)?

hope you guys understand my point.

thanks

Guilherme Borges Lima
Greenhorn

Joined: Jun 04, 2008
Posts: 11
The reference is copied to the method, the Object is the same, and after the method ends the Integer Object is still referred to within the ArrayList, this is why the Integer Object is not eligible for garbage collection
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Guilherme Borges Lima wrote:The reference is copied to the method, the Object is the same, and after the method ends the Integer Object is still referred to within the ArrayList, this is why the Integer Object is not eligible for garbage collection


Integer object is stored in stack [since its local variable, local to the method] ... once the method completes its execution the stack which contains the method calls and the local variables is deleted/destroyed, hence the integer object is gone than.

first of all i want you to confirm that whether Integer object "in" is local variable or not?
if yes, than all local variables are stored in the stack, instance variables on the heap?
Once method is completed what happens to the stack, if the stack is gone, than how come Integer object is still alive?

regards,

Note: even i was thinking the same way that reference gets copied, but the main question is "Integer in" is stored in stack or heap, if heap than i would agree with you saying that it will be alive, but it is declared within a method and its local object and hence no heap and does that mean the stack is not destroyed though method is over.
Guilherme Borges Lima
Greenhorn

Joined: Jun 04, 2008
Posts: 11
The local reference is stored on the stack and the Object is stored on the heap
"in" is a local reference variable
It refers to a Integer Object on the heap
When you add "in" to ArrayList, there are two references to the same Integer Object
When doStuff() finishes, there are one reference to the Integer Object on the heap, because the local reference was destroyed
The reference within the ArrayList, make the Integer Object not eligible for GC.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, Rafi Fareen.. Garbage Collection is for Objects not for variables. Object are created in the heap itself, whether they were created as instances(member) or local.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

The variables(bit pattern holders) only saved in stack.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Rafi, read again what Guilherme wrote
The local reference is stored on the stack and the Object is stored on the heap

So basically if you have a local reference variable, the variable itself will be stored in the stack, but the actual object to which it points is stored in the heap. Instance variables are part of an object, and since the object itself is in the heap, so they are also on the heap. Coming to the original program, when the first statement in the doStuff method is executed, this is what happens
So basically the in variable in the stack points to an object in the heap. Now at the last statement of the doStuff method, the program is in the following state
After the doStuff method is over, the reference variable in which was in the stack is gone, but the ArrayList still points to the Integer object with the value 99...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

yea....
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Ankit Garg wrote:Rafi, read again what Guilherme wrote
After the doStuff method is over, the reference variable in which was in the stack is gone, but the ArrayList still points to the Integer object with the value 99...


Wow, that was really nice presentation ... i understood the concept throughly.

Thanks everyone,

Rafi
 
wood burning stoves
 
subject: Store Local Variable In Instance Variable