Instance Variable vs Static Variable
In a cricket team, each player has their own runs scored, but the team has one total score on the board. If you stored the team total inside each player, you'd have eleven different "totals" and none of them right. Fields work the same way: instance variables hold each object's own data, and static variables hold the one value the whole class shares. Choosing the wrong one is a classic bug.
Two Counters
public class Gates {
static class Gate {
int passed;
static int totalPassed;
void tap() {
passed++;
totalPassed++;
}
}
public static void main(String[] args) {
Gate a = new Gate();
Gate b = new Gate();
a.tap(); a.tap(); a.tap();
b.tap();
System.out.println("Gate A: " + a.passed);
System.out.println("Gate B: " + b.passed);
System.out.println("Station total: " + Gate.totalPassed);
}
}Gate A: 3
Gate B: 1
Station total: 4How it works
- Line 3:
passedis an instance variable: each gate has its own. - Line 4:
totalPassedis a static variable: one value for the class. - Each
tap()adds 1 to that gate'spassedand 1 to the shared total.
Try it yourself: make passed static too. What do the three lines print now?
Side by Side
| Instance variable | Static variable | |
|---|---|---|
| Declared | In the class, without static | In the class, with static |
| Copies | One per object | One per class |
| Created | When an object is created with new | When the class is initialized |
| Destroyed | When the object is garbage collected | When the class is unloaded (usually at program end) |
| Lives in | The object, on the heap | Class data (method area) |
| Accessed as | object.field | ClassName.field |
| Usable from static methods | Only through an object | Yes |
| Default value | Yes (0, false, null) | Yes (0, false, null) |
| Typical use | Each account's balance, each ticket's berth | A counter, a constant, a shared setting |
Constants Are Static Too
Values that never change and are the same for every object should be static final, not instance fields:
| Instead of | Write |
|---|---|
final double gstRate = 0.18; in every object | static final double GST_RATE = 0.18; once for the class |
Common Mistakes
Storing per-object data in a static variable. Every object would overwrite the same value.
Storing a shared counter in an instance variable. Each object would count only itself.
Accessing static variables through objects. a.totalPassed works but hides the fact that it's shared; write Gate.totalPassed.
Interview Corner
What is the difference between an instance variable and a static variable?
An instance variable belongs to an object: each object has its own copy, created with the object. A static variable belongs to the class: there's one copy shared by all objects, created when the class is initialized. Instance variables are accessed through an object; static variables through the class name.
Practice
- Easy: In a
Studentclass, decide which should be static:name,rollNo,schoolName,studentCount. - Harder: Write a
Ticketclass where each ticket gets a uniqueid(instance) taken from a staticnextIdcounter that starts at 1001.
Key Takeaways
Was this tutorial helpful?