static Keyword in Java – Variables, Methods, Blocks and Classes
The static keyword makes a member belong to the class itself rather than to individual objects. A static variable has one shared copy for the whole class, a static method can be called without creating an object, and a static block runs once when the class is loaded.
This tutorial covers static variables, methods, blocks and nested classes, static imports, how static members are stored, and the rules and pitfalls of using static.
1. What static Means
| Can be static | Effect |
|---|---|
| Variables (fields) | One copy shared by all objects of the class |
| Methods | Called on the class; no object needed; no access to this |
| Blocks | Run once, when the class is initialised |
| Nested classes | Do not need an instance of the outer class |
| Imports | Use static members without the class name |
Local variables and top-level classes cannot be static.
2. Static Variables
A static variable is shared by every object of the class. Here a counter of issued tickets is shared, while each ticket keeps its own number:
public class StaticVariable {
static class Ticket {
static int issued = 0; // one copy shared by all Ticket objects
static final String EVENT = "Tech Fest 2026";
final int number; // one copy per object
Ticket() {
issued++;
number = issued;
}
}
public static void main(String[] args) {
Ticket t1 = new Ticket();
Ticket t2 = new Ticket();
Ticket t3 = new Ticket();
System.out.println("t1=" + t1.number + ", t2=" + t2.number + ", t3=" + t3.number);
System.out.println("Tickets issued for " + Ticket.EVENT + ": " + Ticket.issued);
}
}
t1=1, t2=2, t3=3 Tickets issued for Tech Fest 2026: 3
- Access static variables through the class name:
Ticket.issued. Using an object reference also works but is misleading. static finalfields are constants, likeMath.PI.- Mutable static variables are global state: they are shared by all threads and all code, so use them sparingly.
3. Static Methods
Static methods are ideal for utilities and calculations that do not depend on an object's state, like Math.max() or Integer.parseInt():
public class StaticMethod {
static class Converter {
private Converter() { } // utility class: no instances
static double celsiusToFahrenheit(double c) { return c * 9 / 5 + 32; }
static double kmToMiles(double km) { return km * 0.621371; }
}
private int instanceCounter = 0;
static void staticHelper() {
// instanceCounter++; // compile error: non-static field from a static context
System.out.println("static methods are called on the class");
}
public static void main(String[] args) {
System.out.println("30 C = " + Converter.celsiusToFahrenheit(30) + " F");
System.out.printf("10 km = %.2f miles%n", Converter.kmToMiles(10));
staticHelper();
StaticMethod obj = new StaticMethod(); // instance members need an object
obj.instanceCounter++;
System.out.println("instanceCounter = " + obj.instanceCounter);
}
}
30 C = 86.0 F 10 km = 6.21 miles static methods are called on the class instanceCounter = 1
- A static method cannot use instance fields or call instance methods directly, and cannot use
thisorsuper. - Static methods are not overridden; a subclass method with the same signature hides the parent's.
mainis static so the JVM can call it without creating an object.
4. Static Blocks
A static initializer block runs once, when the class is first loaded and initialised, before main or any other use. It is useful for complex initialisation of static fields:
import java.util.Map;
import java.util.HashMap;
public class StaticBlock {
static final Map<String, Integer> COUNTRY_CODES;
static { // runs once, when the class is first used
System.out.println("static block: loading country codes");
Map<String, Integer> m = new HashMap<>();
m.put("IN", 91);
m.put("US", 1);
m.put("JP", 81);
COUNTRY_CODES = Map.copyOf(m);
}
public static void main(String[] args) {
System.out.println("main starts");
System.out.println("India: +" + COUNTRY_CODES.get("IN"));
System.out.println("Japan: +" + COUNTRY_CODES.get("JP"));
}
}
static block: loading country codes main starts India: +91 Japan: +81
If a static block throws an exception, the class fails to initialise with ExceptionInInitializerError, so keep them simple.
5. Static Nested Classes and Static Imports
import static java.lang.Math.max;
import static java.lang.Math.PI;
public class StaticNestedAndImport {
// A static nested class does not need an instance of the outer class
static class Point {
final int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
static Point origin() { return new Point(0, 0); } // static factory method
@Override public String toString() { return "(" + x + ", " + y + ")"; }
}
public static void main(String[] args) {
Point p = new StaticNestedAndImport.Point(3, 4);
System.out.println(p + " and origin " + Point.origin());
// static import: use Math.max and Math.PI without the class name
System.out.println("max(7, 12) = " + max(7, 12));
System.out.printf("PI = %.4f%n", PI);
}
}
(3, 4) and origin (0, 0) max(7, 12) = 12 PI = 3.1416
- A static nested class is like a normal class placed inside another for organisation; it does not hold a reference to an outer object.
- Static imports let you write
max(7, 12)instead ofMath.max(7, 12). Use them sparingly to keep code clear.
6. How Static Members Are Stored
Static fields belong to the class's runtime data, created when the class is initialised and kept as long as the class is loaded, usually for the whole life of the program. Instance fields live inside each object on the heap and disappear when the object is garbage-collected. Holding large objects in static fields can therefore cause memory leaks.
7. Static vs Instance Members
| Aspect | Static | Instance |
|---|---|---|
| Belongs to | The class | Each object |
| Copies | One | One per object |
| Accessed via | ClassName.member | object.member |
Can use this | No | Yes |
| Lifetime | While the class is loaded | While the object is reachable |
| Overriding | Not possible (hiding only) | Possible |
8. When to Use static
- Constants:
static finalvalues. - Utility methods that only use their parameters.
- Factory methods such as
List.of()andLocalDate.now(). - Counters or caches shared across objects (thread-safely).
- Nested helper classes that do not need the outer object.
Avoid static when behaviour depends on object state, when you need polymorphism, or when global state would make testing hard.
9. Summary
staticmembers belong to the class, not to objects.- Static variables are shared; static methods need no object and cannot use
this. - Static blocks run once when the class is initialised.
- Static nested classes need no outer instance; static imports shorten references.
- Use static for constants, utilities and factories; avoid mutable global state.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is the static keyword in Java?
A modifier that makes a variable, method, block or nested class belong to the class itself instead of to individual objects.
Q2. What is a static variable?
A field declared with static. There is only one copy, shared by all objects of the class, and it can be accessed with the class name.
Q3. Can a static method access instance variables?
Not directly, because there is no current object. It can access them through an object reference passed in or created inside the method.
Q4. Why is the main method static?
So the JVM can call it to start the program without first creating an object of the class.
Q5. Can static methods be overridden?
No. A subclass can declare a static method with the same signature, which hides the parent's method, but calls are resolved by the reference type at compile time, not polymorphically.
Q6. When does a static block run?
Once, when the class is initialised, which happens the first time the class is actively used (an object is created, a static method is called or a static field is accessed).
Q7. Can a class be static?
Only a nested class. A top-level class cannot be declared static.
Q8. Can local variables be static?
No. Local variables live only during a method call, so static is not allowed on them.
Q9. Can we use this in a static method?
No. this refers to a current object, and static methods have none.
Q10. What is a static import?
An import that brings static members into scope, such as import static java.lang.Math.*;, so you can write sqrt(16) instead of Math.sqrt(16).
Q11. Are static variables thread-safe?
No. They are shared by all threads, so updates need synchronization or atomic classes such as AtomicInteger. Immutable static constants are safe.
Q12. What is the difference between static and final?
static means "belongs to the class"; final means "cannot change". Combined, static final creates a class-level constant.
Q13. Where are static variables stored?
With the class's data managed by the JVM (in HotSpot, static fields are stored with the class's mirror object on the heap since Java 8). They live as long as the class is loaded.
Q14. Can a static method call a non-static method?
Only through an object: new MyClass().instanceMethod(). It cannot call it directly by name.
Q15. Can interfaces have static methods?
Yes, since Java 8. They are called with the interface name, for example Comparator.naturalOrder(), and are not inherited by implementing classes.
Q16. Can a constructor be static?
No. Constructors create objects, so they are inherently tied to instances.
Q17. Why are utility classes made of static methods?
Their methods depend only on their arguments, not on any object state, so creating an object would serve no purpose. Examples are Math, Arrays and Collections.
Q18. Is overusing static bad?
Yes. Mutable static state makes code hard to test and reason about, hides dependencies, and causes thread-safety problems. Prefer passing objects explicitly or using dependency injection.
Q19. Can a static nested class access outer class members?
It can access the outer class's static members directly, including private ones, but needs an object reference to access instance members.
Q20. What is the order of static initialisation?
Static fields and static blocks run in the order they appear in the class, once, when the class is initialised. A parent class is initialised before its subclass.