static Keyword in Java — Fields, Methods, Blocks, and Nested Classes
Fast recall
Quick revision
Core answer
static makes a member associated with its class rather than with a particular instance. It can be used on fields, methods, initialization blocks, and nested classes; static imports are a separate import feature. A static method has no this and cannot directly use instance members. Class initialization and lifetime are controlled by the JVM and class loader, not simply 'once for the whole program.'Understanding of class-level versus instance-level members — a foundational distinction in nearly every Java codebase
Trying to access instance variables from a static method (compile error), or thinking static methods are overridden polymorphically when they're actually hidden
Explain static initializer blocks that run once at class load, that static methods are hidden not overridden, and give Math/Collections as real-world utility examples
Interview-ready guide
Interview answer
static means a member is associated with the class rather than with each object. For example, all Account instances loaded by the same class loader can update one static int count, while each account has its own non-static balance. A static method can be called with the class name and has no this, so it needs an explicit object reference to use instance state.
The keyword also applies to initialization blocks and nested classes. A static block runs during class initialization, and a static nested class can be created without an outer instance. I would not say static state exists once for the entire JVM or always until process exit: class loaders can create distinct loaded classes, and their classes can later be unloaded.
static Keyword in Java — Fields, Methods, Blocks, and Nested Classes
Direct Answer
static makes a member associated with its class rather than with a particular instance. It can be used on fields, methods, initialization blocks, and nested classes; static imports are a separate import feature. A static method has no this and cannot directly use instance members. Class initialization and lifetime are controlled by the JVM and class loader, not simply 'once for the whole program.'
Code Example
class BankAccount {
private static int nextNumber = 1_000;
private final int number;
static {
validateConfiguration(); // runs during BankAccount initialization
}
BankAccount() {
number = nextNumber++;
}
static int issuedCount() {
return nextNumber - 1_000;
}
static class Summary { } // no BankAccount instance required
}
This demonstrates the four positions. It does not make nextNumber++ thread-safe; class-level mutable state still needs a concurrency policy when several threads construct accounts.
Common Mistakes & Pitfalls
- Saying one copy for the whole JVM: the value belongs to a particular loaded class; class loaders can load separate copies.
- Confusing loading with initialization: a static block runs during class initialization under JVM rules.
- Saying a static method cannot touch objects: it can use an object passed or obtained explicitly; it simply has no implicit receiver.
- Calling static state automatically thread-safe: shared mutable fields still need coordination.
- Calling a static nested class an inner class: an inner class is specifically non-static and carries an enclosing instance.
Check your understanding
Answer these without looking back at the article.
- Can a static method access an instance variable?
- What is a static initializer block?
- Can static methods be overridden in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.