Inner (Nested) Classes in Java
An inner class is a class declared entirely inside another class, and Java has several distinct kinds with different rules. A non-static (regular) inner class is tied to a specific instance of its enclosing class, and can freely access that enclosing instance's fields and methods directly; a static nested class behaves more like an ordinary top-level class that simply happens to be namespaced inside another one, with no automatic tie to any specific enclosing instance at all.
Nesting a class inside another is typically a deliberate signal that the inner class only really makes sense in the context of its enclosing class. A Node class that only exists to support a LinkedList's internal structure is a natural fit for a nested class, rather than being a genuinely independent, standalone concept of its own.
A non-static inner class
Declared without static, tied to a specific instance of its enclosing class, created through that instance.
Direct access to the enclosing instance
A non-static inner class can freely read and call the enclosing object's fields and methods.
A static nested class
Behaves like an ordinary class, just namespaced inside another — it has no automatic connection to any specific enclosing instance.
Creating each kind
A non-static inner class needs an existing enclosing instance to be created from; a static nested class doesn't.
When nesting makes sense
When the inner class's purpose is tightly bound to its enclosing class, like a helper structure that only that outer class actually uses.
public class Outer { String message = "Hello from Outer"; class Inner { void display() { System.out.println(message); } } public static void main(String[] args) { Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.display(); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.