Where does class, object, reference variable get stored in Java. In heap or in stack? Where is heap or stack located?


I understand that variables of a method are stored in stack and class variables are stored in heap. Then where does the classes and objects we create get stored in Java?


Runtime data area in JVM can be divided as below,

  1. Method Area : Storage area for compiled class files. (One per JVM instance)

  2. Heap : Storage area for Objects. (One per JVM instance)

  3. Java stack: Storage area for local variables, results of intermediate operations. (One per thread)

  4. PC Register : Stores the address of the next instruction to be executed if the next instruction is native method then the value in pc register will be undefined. (One per thread)

  5. Native method stacks : Helps in executing native methods (methods written in languages other than Java). (One per thread)