Static fields & methods in Java
One copy per class shared by all instances.
It belongs to the class
A static member belongs to the class itself, not to any object. A static field has exactly one copy, shared by every instance. Picture a classroom: each student has a notebook (instance field), but there's one whiteboard (static field).
class Ticket {
static int count; // one, shared
int id; // one per ticket
Ticket() { id = ++count; }
}Counting visitors
What does this print?
class Visitor {
static int total; int n;
Visitor() { total++; n = total; }
}
void main() {
var a = new Visitor(); new Visitor();
var c = new Visitor();
System.out.println(a.n + " " + c.n);
System.out.println(Visitor.total);
}1 3 33 3 31 3 1
Show the answer
1 3, then 3. total is shared, so it climbs 1, 2, 3. n is per object: each visitor copied the total at the moment it was created.
Static methods have no object
A static method runs without an object, so it has **no this** and can't touch instance fields directly. Call it through the class name: Math.max(a, b), Ticket.count.
Reaching for an instance field
From a static method there's no object whose speed you could mean — so this doesn't compile: "non-static variable speed cannot be referenced from a static context".
class Car {
int speed = 10;
static int getSpeed() {
return speed; // error!
}
}A null that doesn't crash?
What does this print?
class Config {
static String mode = "dark";
}
void main() {
Config c = null;
System.out.println(c.mode);
}darknullThrows NullPointerException
Show the answer
dark! Static members are resolved by the variable's declared type at compile time — the object (here: none) is never consulted. Legal, but confusing: always write Config.mode.
When should a method be static?
You're writing a method. When does static fit?
Think about it, then reveal the answer
When it uses no instance state — it depends only on its parameters, like Math.max(a, b) or Integer.parseInt(s). If it needs an object's fields, make it an instance method. (Static is not a speed trick.)
In real projects
Static is perfect for constants (static final), counters and stateless utilities. Its dark side: mutable static state is shared across every thread and every user of a server, and a static collection that only grows is a classic memory leak.
Key takeaways
- static field: one shared copy per class
- static method: no object, no this
- Call as ClassName.member()
- Good for counters, constants and stateless utilities
Classic Java needs public static void main so the JVM can call it without creating an object first. Java 25's compact source files finally allow a plain instance void main() — the one used in these lessons.
Practice questions
What does this print?
class Ticket {
static int count = 0;
int id;
Ticket() { count++; id = count; }
}
void main() {
var a = new Ticket(); var b = new Ticket();
System.out.println(a.id + " " + b.id);
System.out.println(Ticket.count);
}- 1 1 1
- 1 2 2
- 1 2 1
- 2 2 2
Check your answer
1 2 2. count is shared, so it goes 1 then 2. id is per object, so each ticket remembers the count at the moment it was created.
What does this print?
class Car {
int speed = 10;
static int getSpeed() {
return speed;
}
}
void main() {
System.out.println(Car.getSpeed());
}- 10
- 0
- Compile error
Check your answer
Compile error. A static method has no object, so there is no speed to read. Referencing an instance field from a static context does not compile.