static
static attaches a field or method to the class, not to an instance. There is one copy per class loader, and you call it without an object.
Detailed Explanation
Instance members exist once per object. Static members exist once for the class. Math.PI and Integer.parseInt are static because they do not need a particular Math or Integer. A created counter that every constructor increments is static because it is about the class as a whole.
Static fields are initialized when the class is initialized, which happens on first active use: constructing an instance, calling a static method, or reading a non-constant static field. The JVM holds a lock during that initialization, so it happens once and is visible to all threads. Static final primitives and strings that are compile-time constants are inlined into callers and may never touch the field at runtime.
A static method has no this. It cannot read instance fields or call instance methods unless you pass an object in. Calling a static method through an instance (obj.parse) compiles and still uses the compile-time type, which misleads readers. Always qualify with the class name.
Static nested classes do not hold an outer instance. They are ordinary classes with a dotted name. Static imports (import static java.lang.Math.min) let you write min without Math., which is fine for a handful of names and noisy for many.
You reach for static for factories (List.of), for true constants, for stateless utilities, and for shared caches that you have designed for concurrency. You avoid mutable static fields. They are global variables: tests leak state, threads race, and you cannot have two configurations in one JVM. Prefer an instance you pass in.
The cost of static mutable state is the hardest kind of coupling. The cost of static utilities is that they cannot be overridden or mocked without extra tools. When behaviour must vary in tests, use an instance of an interface instead of a static method.
Code Examples
public class Ids {
private static int next = 1; // one counter for the class
private final int id;
public Ids() {
this.id = next++;
}
public int id() {
return id;
}
public static int peekNext() {
return next;
}
public static void main(String[] args) {
Ids a = new Ids();
Ids b = new Ids();
System.out.println(a.id() + "," + b.id()); // prints: 1,2
System.out.println(Ids.peekNext()); // prints: 3
}
}public final class Maths {
private Maths() {} // no instances; the constructor is a locked door
public static int clamp(int value, int lo, int hi) {
return Math.max(lo, Math.min(hi, value));
}
public static void main(String[] args) {
System.out.println(Maths.clamp(15, 0, 10)); // prints: 10
}
}Common Gotchas
Mutable static fields are shared across tests and threads
reset them in tests or, better, do not have them. Use `AtomicInteger` if the counter must be concurrent.
Calling static methods on an instance hides the real type
`child.parse(...)` still runs `Parent.parse` if that is the declared type.
Class initialization is lazy and synchronized
a slow static block stalls every thread that first uses the class.
`this` in a static method does not compile
pass the instance in, or make the method an instance method.