🏗️ Classes & Objects · Intermediate

Static fields & methods in Java

One copy per class shared by all instances.

🧩 The mysteryEvery ticket needs a unique number: 1, 2, 3... But each ticket object only knows about itself. So who keeps count?

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

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. 1 3 3
  2. 3 3 3
  3. 1 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.

⚠️ The trap

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

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);
}
  1. dark
  2. null
  3. Throws 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.

🤔 Think first

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 the real world

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

  1. static field: one shared copy per class
  2. static method: no object, no this
  3. Call as ClassName.member()
  4. Good for counters, constants and stateless utilities
🤯 Did you know?

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. 1 2 2
  3. 1 2 1
  4. 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());
}
  1. 10
  2. 0
  3. 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.

Next: code that runs before your constructor — and some that runs before your first object even exists.