my dog learned polymorphism*
The moose likes Java in General and the fly likes Where do class level reference variable get stored Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Where do class level reference variable get stored" Watch "Where do class level reference variable get stored" New topic
Author

Where do class level reference variable get stored

Nikita Dutt
Greenhorn

Joined: Dec 24, 2010
Posts: 20
Hi All,

Have a small question where do reference variable at the class level gets stored ; for example



Hope i make sense ; thanks in advance

Regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
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.
Abhijeet Sutar
Greenhorn

Joined: Feb 28, 2010
Posts: 17

Nikita Dutt wrote:Hi All,

Have a small question where do reference variable at the class level gets stored ; for example



Hope i make sense ; thanks in advance

Regards


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.

Hope this helps..


OCPJP 6 93%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Please SearchFirst. This question has been asked and answered a few times before.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nikita Dutt
Greenhorn

Joined: Dec 24, 2010
Posts: 20
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
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.
Nikita Dutt
Greenhorn

Joined: Dec 24, 2010
Posts: 20

Hi All,

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.



Hope i make sense


Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where do class level reference variable get stored