Reuben Reuben - 4 years ago 141
Java Question

What is the actual memory place for static variables?

Static variable is allocated for the entire duration of program's execution, so neither stack
nor heap are convenient for it. Then where is it? There should be some place where it is loaded ?

Answer Source

Static fields are initialised when a class is loaded and and are discarded when the classloader for that class is unloaded. They can be cleaned up, even duplicated in another class loader.

For applications like those this use OSGi, static variables don't live for the life of the application can be reloaded many times.

How this is implement may be JVM dependant but the Sun/Oracle JVM creates an "object" to hold the static fields for a class. This object is accessible via the Unsafe class which can also be used to examine this "objects" fields.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download