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 static variables live

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects and their Instance variables ------> live on heap
method (currently running and the whole method stack through which it was called) and local variables ---------> live on stack

What about static variables? Where exactly are they? After all, you can access the value without existance of any instance on the heap.

Also, related to this, in the following statement "Static variables are initialized the first time a class is loaded into memory", what memory is do they mean? Where is this class (not an instance but the class itself) information? I'm guessing the static variables live wherever this class information is loaded into.

Thanks in advance,
Nidhi
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the BCEL handbook which is probably out of date, but still useful.

As far as I can remember, the values of static fields live inside the Class objects: primitives as values, reference types as pointers to the heap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic