Skip to main content

The Static Keyword

Medium

Interview Question: "What does the 'static' keyword actually mean, where are static members stored in memory, and why can't you override a static method? Also, why should you prefer static nested classes over inner classes?"

The Quick Answer​

"The static keyword denotes that a member (variable, method, or block) belongs to the class itself rather than to any specific object instance. There is only a single copy shared across all instances of that class, loaded once by the JVM."


Static vs. Instance Members​

FeatureInstance MemberStatic Member
Belongs ToSpecific object instance on the heapThe class definition
Invocationobject.doSomething()ClassName.doSomething()
Data AccessCan access both instance and static fieldsCan only access static fields (cannot use this)
Binding MechanismLate Binding (resolved at runtime via vtable)Early Binding (resolved at compile-time by reference)

Where is static Stored in Modern Java? (Metaspace vs. Heap)​

A common senior-level trap explores where static memory lives:

  • Java 7 and earlier: Static variables and class metadata lived in PermGen (Permanent Generation) inside the JVM heap.
  • Java 8+: PermGen was removed and replaced by Metaspace, which resides in native off-heap memory. Class metadata and method bytecodes are stored in Metaspace.
  • The Catch: Static variables themselves (and the java.lang.Class object) are stored in the standard Java Heap. They remain subject to garbage collection if their ClassLoader is unloaded.

Can You Override a Static Method? (Method Hiding)​

The definitive answer is NO. You cannot override static methods in Java:

  • Overriding relies on Dynamic Method Dispatch (late binding), where the runtime inspects the actual object on the heap to decide which implementation to run.
  • Static methods rely on Static Binding (early binding). The compiler inspects the declared reference type at compile-time and hardcodes the method call.
  • If a subclass declares a static method with the exact same signature as a parent static method, it is called Method Hiding, not overriding:
Parent p = new Child();
p.printRules(); // Prints "Parent Rules" because 'p' is typed as Parent!

The Architecture Trap: Static Nested Class vs. Inner Class​

Interviewers frequently ask why Joshua Bloch's Effective Java (Item 24) insists on: "Favor static member classes over non-static."

  • Non-Static Inner Class: Every instance of a regular inner class silently stores an implicit reference to its enclosing outer instance (Outer.this). If the inner class object persists (e.g., in a background thread or event queue), it prevents the outer class from being garbage collected, causing severe memory leaks.
  • Static Nested Class: Does not hold an implicit reference to the outer instance. It behaves like any top-level class, saving memory and eliminating leak risks.

Clean Code Example​

Here is how static variables, methods, and blocks are declared across languages:

public class Bank {
// 1. Static Variable: Shared by all bank accounts
public static double baseInterestRate;

// 2. Static Initializer Block: Runs once when class is loaded
static {
baseInterestRate = 0.045; // 4.5%
}

// 3. Static Utility Method: No access to instance state
public static double calculateInterest(double principal) {
return principal * baseInterestRate;
}

// 4. Static Nested Class: Does not leak outer instance reference
public static class TransactionLog {
public void log(String message) {
System.out.println("Log: " + message);
}
}
}