Is an instance variable of an object in Java stored on the stack or method area of the JVM?
Also, do we have different instance variable for multiple threads?
Most of the JVM implementation divides memory into following parts:
Lets talk about Method Area, Stack and Heap only.
For e.g Take a class
class Lava {
int i = 5;
static int j = 10;
void flow() { //some implementation}
}
When an instance of this object is created from a class X
Lava l = new Lava();
First, Class type of Lava, i.e. Lava.class
is stored in your Method area, with details like methods, fields and other referencing type. Also static variables like j in our example is stored in Method area itself.
Second the instance of Object Lava is stored in Heap Area as well as its instance variable i.e i.
Third, Its reference, i.e l in our example is stored in Stack area, which point to instance that is created in Heap.