Static Keyword
The static keyword marks a field, method, or block as belonging to the class itself, rather than to any one object created from that class. A static member is shared by every instance instead of each instance getting its own copy.
Static Fields — Shared Across All Instances
Every object of a class normally gets its own copy of each instance field. A static field is different — there is only ever one copy, shared by every object. A classic example is a counter that tracks how many objects have been created:
public class Student {
static int count = 0; // shared by every Student object
String name;
Student(String name) {
this.name = name;
count++; // every constructor call increments the shared counter
}
}
// elsewhere
Student s1 = new Student("Aman");
Student s2 = new Student("Priya");
System.out.println(Student.count); // prints 2
Both s1 and s2 update the same count field. That's why the output is 2 after two constructor calls — count doesn't belong to either object, it belongs to the Student class.
Student.count), not through an object reference, even though Java will technically let you do the latter.Static Methods — Called on the Class Itself
A static method can be called directly on the class, without creating an object first. You've already been using static methods without necessarily noticing:
Math.sqrt(16); // static method on the Math class
Integer.parseInt("42"); // static method on the Integer class
Neither call needed a new Math() or new Integer() object — they're called straight on the class name. This is also why main is declared static: the JVM needs to run it before any object of your class exists.
public static void main(String[] args) {
// JVM calls this before creating any objects, so it must be static
}
Static Blocks
A static block runs exactly once, when the class is first loaded by the JVM — before main runs and before any object is created. It's typically used to initialize static fields that need more than a simple assignment.
public class Config {
static String version;
static {
version = "1.0.0"; // runs once, when Config is first loaded
}
}
Why Static Methods Can't Use Instance Members Directly
A static method belongs to the class, and can be called before any object exists. Instance (non-static) fields and methods only exist once an object has been created with new. So inside a static method, there is no guaranteed object to refer to — the compiler simply won't let you reference an instance field or call an instance method directly.
public class Demo {
int value = 10; // instance field
static void show() {
// System.out.println(value); // compile error: can't access instance field here
}
}
To use an instance field from a static context, you'd first need an actual object — for example new Demo().value.
Quick recap:
- static field — one shared copy per class, not per object.
- static method — callable on the class name, no object required.
- static block — runs once, when the class is loaded.
- Static code cannot directly touch instance fields/methods, since no object is guaranteed to exist.