The static keyword in Java marks a member as belonging to the class itself instead of to any single object. A static variable has one shared copy, a static method can be called without creating an object, and a static block runs once when the class is loaded. Use it for data and behaviour that do not depend on one particular instance.
This guide covers static variables, static methods, static blocks, static nested classes and the mistakes beginners make with them. The examples work on Java 21 and Java 25 LTS.
Normally every object has its own copy of each field. When you add static, there is exactly one copy, stored with the class, and every object (and the class name) refers to it.
| Member | Belongs to | Needs an object? | Copies in memory |
|---|---|---|---|
| Instance field | Each object | Yes | One per object |
| Static field | The class | No | One in total |
| Instance method | Each object | Yes | Shared code, per-object data |
| Static method | The class | No | Shared code, no object data |
You access static members through the class name, for example Math.max(3, 9) or Integer.MAX_VALUE. That is why you never write new Math().
A static variable is useful when all objects must see the same value, such as a counter of how many objects were created.
Each constructor call increments the same count. Because the variable is private, other classes read it only through getCount().
Constants are the most common use of static. Combine static and final and name the field in upper case:
Mutable static state is shared by the whole program, including all threads. Changing it from several threads without synchronisation causes race conditions, so prefer constants and keep mutable statics rare.
A static method is called on the class and receives no hidden this reference. That leads to the most important rule: a static method can use only static members directly. It cannot touch instance fields or call instance methods unless you give it an object.
The most famous static method is public static void main(String[] args). The Java launcher must run it before any object exists, so it has to be static. Since Java 25 you can also write compact source files with an instance main, but the classic form is what you will see in almost all code.
A static method cannot be overridden, only hidden by a method with the same signature in a subclass, so it does not take part in runtime polymorphism.
A static block runs once, when the JVM initialises the class (usually on first use). Use it when a static field needs more than one line of setup.
When a class has several static fields and blocks, they run from top to bottom in the order written. Instance initialisers and constructors run later, each time you create an object. If a static block throws an exception, the class fails to load with ExceptionInInitializerError, so keep these blocks short and safe.
A nested class declared static does not hold a reference to an outer object, so you can create it without an outer instance.
A static import lets you use static members without the class name:
Use static imports sparingly, because they make it harder to see where a name comes from.
main directly. The compiler reports that a non-static method cannot be referenced from a static context. Create an object first.Student shows the same name, the field should not be static.Student.getCount() and not s1.getCount().The JVM needs to start the program before any object exists. A static method can be called on the class itself, so main is the entry point.
Not directly, because there is no current object. It can access them through an object reference that you pass in or create inside the method.
It runs once, when the class is initialised, which normally happens at first use such as creating an object or calling a static method.
No. A constructor creates an instance, so it always belongs to an object being built. Use a static factory method if you want a named creation method.
Static members belong to the class and exist once, regardless of how many objects you create. Use static final for constants, static methods for stateless utilities, and static blocks for one-time class setup. Remember that static code cannot reach instance members without an object, and keep mutable static state to a minimum.