The static Keyword in Java
The static keyword in Java means "this belongs to the class, not to any one
object". A static variable is shared by every object, and a static method can be called without creating an
object at all. Once you see the difference between class-level and object-level, a lot of Java suddenly makes sense.
What does static mean in Java?
Think of a classroom. Each student has their own notebook (instance variables), but there's only one whiteboard at the front that everyone shares. If the teacher writes on it, every student sees the same thing. Static members are the whiteboard.
public class Ticket {
static int created = 0; // ONE copy, shared by all tickets (the whiteboard)
final int number; // each ticket has its OWN number (a notebook)
Ticket() {
created++;
number = created;
}
}
Ticket a = new Ticket();
Ticket b = new Ticket();
Ticket c = new Ticket();
System.out.println(a.number + ", " + b.number + ", " + c.number);
System.out.println("Tickets created: " + Ticket.created); 1, 2, 3 Tickets created: 3
static int created— exists once, on the class. All three constructors incremented the same counter.final int number— every ticket gets its own copy with its own value.Ticket.created— static members are accessed through the class name, not an object.
What can be static in Java?
6 uses of the static keyword
static variable
One copy shared by every object of the class. Change it once and everyone sees the change.
static int created = 0; // shared counter static method
Called on the class, no object needed. Can't use this or instance fields directly.
static double toFahrenheit(double c) {
return c * 9 / 5 + 32;
}
// Temperature.toFahrenheit(25) static final
A constant: one shared copy that never changes. Named in UPPER_SNAKE_CASE.
public static final double VAT_RATE = 0.18; static block
Runs once, when the class is first loaded, to set up static data that needs more than one line.
static final Map<String, Integer> CODES = new HashMap<>();
static {
CODES.put("LK", 94);
CODES.put("US", 1);
} static nested class
A class inside a class that doesn't need an outer object. Often used for builders and helper types.
class Pizza {
static class Builder { /* ... */ }
}
// new Pizza.Builder() static import
Lets you use static members without the class name prefix.
import static java.lang.Math.max;
// max(3, 7) instead of Math.max(3, 7) Static methods: no object required
If a method doesn't need any object's data, make it static. You've used plenty already:
Math.max(3, 7), Integer.parseInt("42"), List.of(1, 2, 3).
public class Temperature {
static double toFahrenheit(double celsius) {
return celsius * 9 / 5 + 32;
}
}
System.out.println(Temperature.toFahrenheit(25)); // no 'new Temperature()' needed 77.0
Why can't a static method use instance variables?
Because it doesn't run "on" any object. If Ticket had a static method that read number,
Java would ask: which ticket's number? There could be zero tickets or a million. The same is true
for this and super,
which also aren't allowed in static code.
| From inside a… | Static members | Instance members |
|---|---|---|
| Static method | ✔ Yes | ✘ No (needs an object reference) |
| Instance method | ✔ Yes | ✔ Yes |
Why is main static?
When your program starts, no objects exist yet. A static main lets the JVM call
YourClass.main(args) straight away, without needing to create a YourClass object first.
That's also why beginners hit this error:
public class App {
void greet() { System.out.println("Hello"); }
public static void main(String[] args) {
greet(); // ✘ non-static method greet() cannot be referenced from a static context
new App().greet(); // ✔ create an object, then call its method
}
} New in Java 25: small programs can now use an instance void main() in a compact source file (JEP 512). The JVM creates the object for you. The classic public static void main(String[] args) is still what you'll see in almost all existing code.
Static blocks: one-time setup
A static block runs once, the first time the class is used, before any static method or object creation:
import java.util.HashMap;
import java.util.Map;
public class DialCodes {
static final Map<String, Integer> CODES = new HashMap<>();
static {
System.out.println("Loading dial codes...");
CODES.put("LK", 94);
CODES.put("US", 1);
CODES.put("GB", 44);
}
static int codeFor(String country) {
return CODES.getOrDefault(country, -1);
}
}
System.out.println(DialCodes.codeFor("LK"));
System.out.println(DialCodes.codeFor("GB")); Loading dial codes... 94 44
Notice "Loading…" printed only once, even though we called codeFor twice.
When should you avoid static?
- Don't make ordinary data static just to avoid creating objects. A static
balancewould mean every account shares one balance! - Mutable static state is shared by all threads, which can cause bugs in multi-threaded code. See thread-safe lists.
- Static methods can't be overridden, only hidden, so they don't take part in polymorphism.
Good uses: constants (static final), pure helper methods (input in, result out), factory methods like List.of(), and counters or caches that truly belong to the whole class.
Quick recap
static= belongs to the class; one shared copy.- Access static members with the class name:
Ticket.created,Math.max(). - Static methods can't use
this,super, or instance members directly. mainis static so the JVM can start your program without creating an object.- Static blocks run once, when the class loads.
What's next?
static is often paired with final to make
constants. Learn what final does next, or browse the full Java keywords list.
Frequently Asked Questions
What does static mean in Java?
static means a member belongs to the class itself rather than to individual objects. There's exactly one copy of a static variable, shared by every object, and a static method can be called without creating an object, like Math.max(3, 7).
Why is the main method static in Java?
When your program starts, no objects exist yet. Making main static lets the JVM call it directly on the class without creating an object first. (Since Java 25, simple programs may also use an instance main method, but static main is still the standard.)
Can a static method access instance variables?
Not directly. A static method has no current object, so it can't know whose instance variable you mean. It can access them through an object reference you pass in or create, like account.balance.
Can static methods be overridden in Java?
No. If a subclass declares a static method with the same signature, it hides the parent's method rather than overriding it. Which one runs depends on the declared type at compile time, not the actual object, so there's no polymorphism.
When should I use static?
Use static for constants (static final), for helper methods that don't depend on any object's state, and for data that is genuinely shared across all objects. Avoid static for ordinary object data, because shared mutable state makes code harder to test and less thread-safe.