# The static Keyword: Variables, Methods, Blocks and Nested Classes

> Explain the static keyword in Java. Where can it be used, and why can't a static method access instance members directly?

Canonical: https://www.internhack.in/learn/interview/java-interview/java-static-keyword
Last updated: 2026-11-28

Difficulty: Beginner

## Question

Explain the static keyword in Java. Where can it be used, and why can't a static method access instance members directly?

## Answer

Let's demystify static. The big idea is simple: a static member belongs to the class itself, not to any particular object. Non-static members belong to each object separately. Once that clicks, every rule about static follows naturally.

Imagine a college. Each student has their own roll number and name; those are instance variables. But the college name and the total number of admitted students are shared by everyone; those are static. You do not need any student to exist to ask for the college name.

There are four places you can apply static. A static variable has exactly one copy, shared by all instances, stored with the class metadata (in Metaspace and the heap for the value, since Java 8). It is initialized when the class is loaded. Classic uses are counters, constants (static final) and shared caches. A static method can be called using the class name without creating an object, like Math.max or Integer.parseInt. A static block runs once when the class is first initialized and is used for complex static setup. A static nested class is a class declared inside another class with the static modifier; it does not hold a hidden reference to an outer instance, unlike an inner class.

Now the question of why a static method cannot use instance members directly. A static method can run without any object, so there is no this. If you refer to an instance field name, the compiler has no idea whose field you mean. Instance methods, on the other hand, can freely use static members because the class always exists. To touch an instance field from a static method, you must have an object reference, for example obj.name.

For the same reason, this and super cannot be used in static context, and static methods cannot be truly overridden. If a subclass declares a static method with the same signature, that is method hiding, not overriding. The method chosen depends on the reference type at compile time, not the runtime object type, so there is no polymorphism.

About initialization order: static blocks and static field initializers run in textual order when the class is first used, which happens on the first instance creation, first static member access, or Class.forName. They run only once per class loader, before any instance code. That is why the main method can start: the JVM loads the class and runs static init before invoking main.

Some pitfalls. Static state is global state, so it makes testing harder and causes thread-safety issues in concurrent code; mutable static collections need synchronization. Static fields also live as long as the class does, so storing large objects or listeners there can cause memory leaks in long-running apps. Also, a static method can be called through a null reference without NullPointerException, since the compiler resolves it by declared type, though this is misleading code.

A final point: the main method is static so the JVM can call it without creating an object of your class. And a class itself can be static only if it is nested; top-level classes cannot be static.

## Code examples

### Shared counter and static block

```
class Counter {
    static int count;
    final int id;

    static {
        System.out.println("static block");
    }

    Counter() {
        id = ++count;
    }

    static int total() {
        return count;
    }
}

public class StaticDemo {
    public static void main(String[] args) {
        System.out.println("start");
        new Counter();
        new Counter();
        System.out.println(Counter.total());
    }
}
```

The static block runs when Counter is first used (at the first new), not when the program starts, and only once. The count field is shared by both objects.

## Key points

- Static members belong to the class and are shared by all objects.
- Static methods have no this, so they cannot access instance members directly.
- Static blocks run once at class initialization.
- Static methods are hidden, not overridden.
- Mutable static state is global and hurts testability and thread safety.

## Common follow-up questions

- Can we override a static method?
- Difference between a static nested class and an inner class?
- When is a static block executed relative to the constructor?
- Why is main declared static?

## Interview tips

- Say the sentence: static belongs to the class, instance belongs to the object. Then derive all rules from it.
- Distinguish overriding from hiding. It is a common trap question.

## Concepts covered

static, Class Loading, Static Block, Nested Class, Memory

Previous: [final vs finally vs finalize](https://www.internhack.in/learn/interview/java-interview/java-final-finally-finalize) · Next: [Wrapper Classes, Autoboxing and the Integer Cache](https://www.internhack.in/learn/interview/java-interview/java-wrapper-autoboxing-integer-cache) · [All of Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)

## More Core Java & Collections questions

- [Checked vs Unchecked Exceptions](https://www.internhack.in/learn/interview/java-interview/java-checked-vs-unchecked-exceptions)
- [try-with-resources and AutoCloseable](https://www.internhack.in/learn/interview/java-interview/java-try-with-resources)
- [The Java Collections Framework Hierarchy](https://www.internhack.in/learn/interview/java-interview/java-collections-hierarchy)
- [ArrayList vs LinkedList](https://www.internhack.in/learn/interview/java-interview/java-arraylist-vs-linkedlist)
- [How HashMap Works Internally](https://www.internhack.in/learn/interview/java-interview/java-hashmap-internals)
- [HashMap vs Hashtable vs ConcurrentHashMap](https://www.internhack.in/learn/interview/java-interview/java-hashmap-hashtable-concurrenthashmap)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)
