What is the difference between static and instance variables in Java?
Static variables (also called class variables) are declared with the static keyword and belong to the class itself rather than any particular object. There is exactly one copy of a static variable in memory, shared by all instances of the class. They are initialized when the class is first loaded by the class loader and persist for the class's lifetime.
Instance variables are declared without static and belong to individual object instances. Each time you create a new object with new, a new copy of each instance variable is allocated. Changes to one object's instance variable have no effect on another object's copy.
A static variable declared as public static int count = 0 in a Counter class is incremented by every Counter object created. Every instance reads and writes the same counter. An instance variable private int id in the same class is unique per object — each counter has its own id.
Static variables are accessed via the class name: Counter.count. While Java allows accessing static members through an instance reference (myCounter.count), this is misleading and discouraged — IDEs and checkstyle tools flag it because it implies the variable is per-instance when it is not.
Memory layout: instance variables are stored in each object's heap allocation. Static variables are stored in the method area (metaspace), associated with the class's Class object.
Common use cases for static variables: constants (public static final String DEFAULT_NAME), singleton instances, shared counters or registries, and cached computed values. Instance variables model per-object state: a Person's name, age, and address.
Thread safety: static variables shared across instances require synchronization or use of AtomicInteger/AtomicReference if multiple threads can modify them. Instance variables are generally only accessed by the thread that owns the object, but this depends on whether the object is shared.
| Aspect | Static Variable | Instance Variable |
|---|---|---|
| Ownership | Belongs to the class | Belongs to each object instance |
| Memory copies | One — shared by all instances | One per object |
| Lifetime | Class loading to class unloading | Object creation to GC |
| Memory location | Method area / Metaspace | Heap (within the object) |
| Access | ClassName.var (or instance.var) | this.var (or just var) |
| Thread safety concern | Yes — shared state across threads | Usually no, if object not shared |
Correctly explains that static variables are shared across all instances and instance variables are per-object, with a concrete example.
Adds memory location (metaspace vs. heap), class loading lifetime for statics, thread safety implications, and discourages accessing static members via instance references.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment