Nested Classes in Java
Java allows you to define a class inside another class, creating what are called nested classes. There are four distinct kinds: static nested classes, inne...
Classes Within Classes: Four Flavors
Java allows you to define a class inside another class, creating what are called nested classes. There are four distinct kinds: static nested classes, inner classes (non-static), local classes (defined inside a method), and anonymous classes (defined and instantiated in a single expression). Each has different rules about what outer-class members it can access and how it must be instantiated. Choosing the right kind depends on how tightly the inner class is coupled to its enclosing context.
Static Nested Classes A static nested class is declared with static inside the outer class. It behaves like a regular top-level class except that its name is scoped inside the outer class. It can only access static members of the outer class—it has no implicit reference to an outer instance. Static nested classes are useful for grouping helper classes logically with their primary class without requiring an outer instance to exist: OuterClass.NestedClass obj = new OuterClass.NestedClass().
Inner Classes (Non-Static) A non-static inner class holds an implicit reference to the enclosing outer instance and can access all members of the outer class, including private ones. Creating an inner class requires an existing outer object: Outer.Inner obj = outerRef.new Inner(). Because each inner instance holds a reference to its outer instance, inner classes can cause memory leaks if they outlive the outer object—a common pitfall with anonymous Runnable or listener implementations.
Local and Anonymous Classes A local class is defined inside a method and can only be used within that method. It can access local variables only if they are effectively final. An anonymous class is a local class without a name, defined and instantiated simultaneously. Anonymous classes are compact but harder to read for complex logic. In modern Java, lambdas replace most anonymous class uses for functional interfaces, leaving anonymous classes mainly for non-functional interface or abstract class implementations.
Code Example: class Outer { private int x = 10;
class Inner { void show() { System.out.println("x = " + x); } // accesses outer's x }
static class StaticNested { void greet() { System.out.println("Hello from static nested"); } } }
Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.show(); // x = 10
Outer.StaticNested sn = new Outer.StaticNested(); sn.greet(); // Hello from static nested
Interview Tip: A well-known interview question involves memory leaks caused by inner classes. If an inner class instance is passed to a long-lived component (like an event bus), it keeps the outer class instance alive even after the outer instance is no longer needed by the application. The fix is to use a static nested class instead, which eliminates the hidden reference to the outer instance.