Where Are Instance Variables Stored?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
An instance variable is a non-static field that belongs to an object. Because objects have heap semantics, the field's value is part of the containing object's heap state. Each new object gets its own instance fields.
For int quantity, the number is stored as part of the OrderLine object. For Product product, the field stores a reference to a separate Product object. Two order lines can therefore have different quantities but point to the same product.
Instance fields receive default values before constructor code runs: numbers start at zero, boolean starts as false, and references start as null. Local variables are different because Java requires them to be assigned before they are read. Static fields are class-level state rather than per-object state.
Changing a reference field changes which object it points to; it does not copy the target object. JVMs choose the exact field offsets, padding, and object layout. The stable rule is that an instance field belongs to its object, while a reference field contains an arrow to another object.
Where Are Instance Variables Stored
Direct Answer
An instance variable is a field that belongs to an object, so its value is stored as part of that object's logical heap state. A primitive field stores its value there; a reference field stores a reference there, while the referenced object is a separate heap object.
Deep Dive & Architecture
Instance fields form object state
When Java creates an object, its instance fields first receive default values: zero for numeric primitives, false for boolean, and null for references. Constructor code then assigns the intended values. Each instance has its own field values, so changing first.quantity does not change second.quantity. Local variables are different: the compiler requires definite assignment before a local is read.
Reference fields create a graph
Source syntax makes order.customer.address look like nested boxes, but the useful runtime model is a chain of reference values between separate objects. Two Order objects can point to the same Customer, and several Customer objects can point to one immutable Address. Each target's lifetime depends on every incoming reachable path.
Copying shows why the distinction matters
A shallow copy creates a new outer object but can copy its reference values unchanged. Both outer objects then reach the same mutable inner list. A deep copy deliberately creates selected inner objects as well. Neither behaviour follows merely from the word “copy”; the API must define it.
A final reference field prevents reassignment after Java's construction rules are satisfied, but it does not freeze the target object. Thread safety and immutability depend on the complete reachable state. Exact headers, padding, compressed references, and field offsets are JVM details; the stable rule is that an instance field belongs to its containing object and a reference field stores an arrow, not the target itself.
Code Example
class InstanceVariables {
static final class Product {
final String name;
Product(String name) { this.name = name; }
}
static final class OrderLine {
int quantity;
Product product;
OrderLine(int quantity, Product product) {
this.quantity = quantity;
this.product = product;
}
}
public static void main(String[] args) {
Product shared = new Product("Book");
OrderLine first = new OrderLine(2, shared);
OrderLine second = new OrderLine(1, shared);
System.out.println(first.quantity + second.quantity);
System.out.println(first.product == second.product);
}
}
Check your understanding
Answer these without looking back at the article.
- Where is a primitive instance field stored?
- Does a reference field contain the referenced object itself?
- How are instance fields different from static fields and local variables?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.