Learn Java
import com.learnjava.oopfundamentals.InstanceStaticVars;

Instance Variables vs Static Variables in Java

@Concept

An instance variable is declared inside a class but outside any method, and every object gets its own independent copy — changing one object's copy never affects another object's copy of that same field. A static variable, marked with the static keyword, belongs to the class itself rather than to any individual object, so there's exactly one shared copy no matter how many objects of that class exist, and every object, and the class itself, sees the same value.

A static variable is a natural fit for data that's genuinely shared across every instance — a running count of how many objects have been created, a shared configuration value, a constant — while an instance variable fits data that's meaningfully different from one object to the next, like each object's own name or balance.

@HowItWorks
1

Instance variables

Declared without static, each object gets its own independent copy, initialized fresh for every new instance.

2

Static variables

Declared with static, exactly one copy exists, shared by the class itself and every one of its instances.

3

Changing a static variable is visible everywhere

Since there's only one copy, updating it through any single object's reference changes what every other object, and the class, sees too.

4

Accessing static members

Conventionally through the class name, like ClassName.variable, though accessing through an instance reference also works and means the same thing.

5

A natural use for static

A running count of created instances, since that value is genuinely about the class as a whole, not about any one object.

@Example
public class Counter {
static int totalCreated = 0;
int id;
 
Counter() {
totalCreated++;
id = totalCreated;
}
 
public static void main(String[] args) {
Counter a = new Counter();
Counter b = new Counter();
System.out.println("a's id: " + a.id);
System.out.println("b's id: " + b.id);
System.out.println("Total created: " + Counter.totalCreated);
}
}
Output
a's id: 1
b's id: 2
Total created: 2
1totalCreated is static, so there's exactly one shared copy across every Counter object ever made.
2Creating a increments that single shared copy to 1, and a's own instance variable id captures that value for itself.
3Creating b increments the same shared copy again, to 2, and b's own id field captures this second value.
4Counter.totalCreated reads that one shared value directly through the class name, correctly showing 2.
@UseCases
Tracking a running total or count shared across every instance of a class
Storing a shared constant or configuration value once, rather than duplicating it per object
Giving each object its own independent piece of state, like a unique ID or a personal balance
Understanding why changing a static field through one object's reference affects every other object too

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.