🧩 Methods · Beginner

Static vs instance methods in Java

static methods belong to the class and cannot use this.

🧩 The mysteryA variable set to null, a method called on it... and no NullPointerException. Java isn't broken: that method never needed the object at all.

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!
    }
}
🔮 Predict it

Your turn

What happens?

class Dog {
    String name = "Rex";
    static String hello() {
        return "I am " + name;
    }
}
void main() {
    System.out.println(Dog.hello());
}
  1. I am Rex
  2. I am null
  3. Compile 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
}
🔮 Predict it

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);
}
  1. 3 20
  2. 2 20
  3. 3 30
  4. 1 10
Show the answer

online is static: one shared copy counts all 3 joins. score is per object, and q joined twice: 20.

⚠️ The trap

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()); // clear
💼 In the real world

In 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

  1. static: Math.max(3, 4), no object needed
  2. instance: name.length(), needs an object to call on
  3. static methods can't use this or instance members directly
  4. Instance methods can freely call static ones
🤯 Did you know?

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?

  1. "hi".length()
  2. Math.sqrt(16)
  3. list.size()
  4. 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);
}
  1. 2 2
  2. 3 2
  3. 3 3
  4. 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.

Next: where do a method's variables live, and why do they forget everything the moment the method returns?