This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where do class level reference variable get stored

 
Nikita Dutt
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SearchFirst. This question has been asked and answered a few times before.
 
Nikita Dutt
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic