Static vs instance methods in Java
static methods belong to the class and cannot use this.
Class vs object
A **static method belongs to the class itself**: call it as ClassName.method(), no object needed, like Math.sqrt(16). An instance method belongs to each object: it needs an object to call on, like "hi".length(), and works with that object's data.
class Temp {
double celsius;
double toF() { // instance
return celsius * 9 / 5 + 32;
}
static double cToF(double c) { // static
return c * 9 / 5 + 32;
}
}No `this` in static
An instance method runs on an object, available as this. A static method runs with no object at all, so it has no this and can't directly use instance fields or call instance methods. The other direction is fine: instance methods can freely call static ones.
class Box {
int size = 3;
static int twice() {
return size * 2; // error!
}
}Your turn
What happens?
class Dog {
String name = "Rex";
static String hello() {
return "I am " + name;
}
}
void main() {
System.out.println(Dog.hello());
}I am RexI am nullCompile error
Show the answer
Compile error: non-static variable name cannot be referenced from a static context. name belongs to each Dog object, but hello() runs without any Dog, so whose name would it read?
Static fields are shared
The same rule applies to fields. A static field has one copy shared by all objects of the class. An instance field has one copy per object.
class Counter {
static int total; // one, shared
int mine; // one per object
}Your turn
Two players, three joins. What prints?
class Player {
static int online; int score;
void join() { online++; score += 10; }
}
void main() {
Player p = new Player(), q = new Player();
p.join(); q.join(); q.join();
int n = Player.online;
System.out.println(n + " " + q.score);
}3 202 203 301 10
Show the answer
online is static: one shared copy counts all 3 joins. score is per object, and q joined twice: 20.
Static call through null
Calling a static method through a variable is resolved by the variable's declared type; the reference itself is never used. So this prints hello even though u is null! Legal, but misleading. Always call static methods on the class name.
class Util {
static String hello() { return "hello"; }
}
Util u = null;
System.out.println(u.hello()); // hello
System.out.println(Util.hello()); // clearIn real projects
Static methods power utility classes (Math, Arrays, Collections) and factories like List.of or Integer.parseInt. Static fields need care: in a web server, a static field is shared by every user and request, so storing "the current user" in one is a classic, dangerous bug.
Key takeaways
- static: Math.max(3, 4), no object needed
- instance: name.length(), needs an object to call on
- static methods can't use this or instance members directly
- Instance methods can freely call static ones
You can't create a Math object: new Math() doesn't compile because Math's constructor is private. It's a pure toolbox of static methods.
Practice questions
Three of these call instance methods. Which one calls a static method?
- "hi".length()
- Math.sqrt(16)
- list.size()
- sb.append("x")
Check your answer
Math.sqrt(16). Math.sqrt is called on the class Math itself. The others are called on a specific object: a string, a list, a StringBuilder.
What does this print?
class Counter {
static int total;
int mine;
void click() { total++; mine++; }
}
void main() {
Counter a = new Counter(), b = new Counter();
a.click(); a.click(); b.click();
System.out.println(Counter.total + " " + a.mine);
}- 2 2
- 3 2
- 3 3
- 1 2
Check your answer
3 2. total is static, so one copy is shared by all Counters and counts all 3 clicks. mine is per object, and a was clicked twice.