This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Search for the BCEL handbook, which explains it better than I would, though it may be out of date.
You haven't got any class variables in that code, only instance variables.
The instance variable is stored in the heap as part of the object, and it points to another object on the heap (at least I think it does). In Java6 that becomes slightly inaccurate.
Have a small question where do reference variable at the class level gets stored ; for example
Hope i make sense ; thanks in advance
At line 12, you are creating the instance variable/field , Which is the reference variable, declaration and initialization ,of class MyClass
When you will create the instance of the ,then that instance will be stored on Heap memory along with all its instance variables/fields i.e. the state of the object
So, reference variable cL will be on heap as along as instance is on heap
and yes reference variable c2 which is declared inside method will be live as long as method code executes.
Sorry Rob for the inconvenience ; thanks to all of you ; esp my fellow greenhorn ; to add to it am i right when i say that the reference variable inside the method will be stored on the stack and the actual object it points to is on heap
Joined: Oct 13, 2005
You didn't ask about local variables. You are nearly right.
There are a few short-lived objects which are created on the stack in Java6. I can't remember any more details.
Joined: Dec 24, 2010
Just to sum up things
Reference variable cL will be stored on heap and will refer to an object of MyClass which will again be on Heap.
Reference variable C2 will be stored on stack and will refer to an object of MyClass which will be on Heap.