• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Where do class level reference variable get stored

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79704
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Fire me boy! Cool, soothing, shameless self promotion:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic