I just know that the non-primitives (the objects) go on the heap, and methods go on the stack, but what about the primitive variables?
–update
Based on the answers, I could say the heap can have a new stack and heap for a given object? Given that the object will have primitive and reference variables..?
Primitives defined locally would be on the stack. However if a primitive were defined as part of an instance of an object, that primitive would be on the heap.
With regards to the update:
Objects do not have their own stack. In my example,
int ywould actually be part of each instance ofHeapClass. Whenever an instance of HeapClass is allocated (e.g.new Test.HeapClass()), all member variables of HeapClass are added to the heap. Thus, since instances ofHeapClassare being allocated on the heap,int ywould be on the heap as part of an instance ofHeapClass.However, all primitive variables declared in the body of any method would be on the stack.
As you can see in the above example,
int xis on the stack because it is declared in a method body–not as a member of a class.