Where Are Objects Stored in Java?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
At the JVM model level, every class instance and array is allocated from the heap. This includes normal objects, records, exceptions, primitive arrays, reference arrays, and String objects.
The variable that points to an object may live somewhere else. In Customer customer = new Customer(), the local frame holds the customer reference, while the Customer object is on the heap. If that reference is returned or stored in a field, the same object can outlive the method.
Fields belong to their containing object. A primitive field stores its value inside that object, and a reference field stores a link to another object. An array is also an object, so int[] values is a reference to a heap array.
An optimizing JVM may use escape analysis to remove a temporary allocation or keep its values in registers. That optimization is invisible and not guaranteed. Java code should use the rule that instances and arrays have heap semantics and should not depend on a physical object location.
Where Are Objects Stored in Java
Direct Answer
Java class instances and arrays are logically allocated from the JVM heap. A local variable can hold a reference in its method frame, and an optimizing JVM may eliminate or decompose an allocation, but Java code cannot depend on a particular physical object address or on “stack allocation.”
Deep Dive & Architecture
Reference variables and their objects
A variable is a storage location holding a value; an object is a runtime entity with identity and fields. In User first = new User(), first holds a reference value. Assigning User second = first copies that reference, not the User. Both arrows reach one object, so a mutation through second is visible through first.
Arrays follow the same rule. int[] numbers is a reference variable, while the array is an object that contains integer elements. An object field can hold another reference, producing a graph rather than physically nesting one complete object inside another. A shallow copy duplicates the outer object but can keep arrows to the same inner targets.
Reachability and object lifetime
Garbage collection does not use the source block where an object was created as its lifetime. A returned reference, static field, instance field, live thread, JNI reference, or other root path can retain the object after the creator returns. Conversely, a JVM can determine that a local is no longer needed before the source method's closing brace.
Treat optimisation as invisible
Escape analysis can prove that a temporary object's identity is not observable. A JIT may then remove the allocation or keep scalar values in registers or implementation storage. The program must behave exactly as if normal heap semantics applied. This is why the safe interview model is “instances and arrays come from the heap; references live wherever their variables or fields live,” followed by optimisation only as a boundary note.
Code Example
class ObjectStorage {
static final class Counter { int value; }
public static void main(String[] args) {
Counter first = new Counter();
Counter second = first;
second.value = 42;
System.out.println(first.value);
System.out.println(first == second);
}
}
Check your understanding
Answer these without looking back at the article.
- Are primitive values always stored on a stack?
- What can escape analysis change without changing Java semantics?
- Does assigning an object reference copy the object?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.