Learn Java
import com.learnjava.oopadvanced.InnerClasses;

Inner (Nested) Classes in Java

@Concept

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.

@HowItWorks
1

A non-static inner class

Declared without static, tied to a specific instance of its enclosing class, created through that instance.

2

Direct access to the enclosing instance

A non-static inner class can freely read and call the enclosing object's fields and methods.

3

A static nested class

Behaves like an ordinary class, just namespaced inside another — it has no automatic connection to any specific enclosing instance.

4

Creating each kind

A non-static inner class needs an existing enclosing instance to be created from; a static nested class doesn't.

5

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.

@Example
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();
}
}
Output
Hello from Outer
1Inner is a non-static inner class, so it needs an existing Outer instance to be created from at all.
2outer.new Inner() creates that Inner instance, tied specifically to outer.
3Inner's display() method reads message directly from its enclosing Outer instance, with no special syntax required.
4The single println call prints that enclosing instance's field value, confirming the connection.
@UseCases
Defining a small helper class that only makes sense in the context of one specific enclosing class
Giving a nested class direct access to its enclosing instance's private fields and methods
Structuring a linked data structure's internal Node class as a nested class of the structure itself
Choosing between a non-static inner class and a static nested class based on whether enclosing-instance access is actually needed

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.