wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Where do static variables live Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where do static variables live" Watch "Where do static variables live" New topic
Author

Where do static variables live

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

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


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37970
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Where do static variables live
 
Similar Threads
Primitive variable
stack, heap, ...
Static final instance variables
where are static variables kept
static intializers