0

Where are static variables stored? Is there any separate memory for the static variables? I know that they are not a part of the object, are they also not part of the Java heap and store d somewhere?

If so isn't it unsecure?

2 Answers2

4

Static members are part of the class object that instantiated the object. The class object is an object, too - and it resides in the heap. Remember: all classes are instances of the Class class!

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • +1 "class object that instantiated the object.The class object is an object". We don't see this information in general answers. – kosa Jun 13 '13 at 19:45
  • JVM never instantiates any object by itself. It makes use of the class loaders to detect the main class and call the main and this is why main is declared to be static. –  Jun 25 '13 at 11:35
2

They are stored in the PermGem part of the JVM.

static Object var= new Object(); 

var is in the PermGen, and the Object instance is in the Heap.

EDIT: The permanent generation (or PermGen) is used for class definitions and associated metadata. Permanent generation is not part of the heap.

darijan
  • 9,725
  • 25
  • 38