OOP · Advanced OOP
Nested Classes
Imagine you're building a Library Management System.
Real-World Problem
Imagine you're building a Library Management System. A Library contains helper classes like Book, Shelf, and Member. These classes are meaningful only within the library and do not need to be exposed as independent classes. Nested classes help organize such related components.

Why Do We Need Nested Classes? Creating separate top-level classes for small helper components makes projects difficult to organize. Nested classes improve code organization, encapsulation, and readability by grouping related classes together. What are Nested Classes?
A Nested Class is a class declared inside another class. It helps logically organize code and allows the inner class to access members of the outer class when required.
Types of Nested Classes
Java provides four types of nested classes, each designed for a specific purpose. Nested classes help organize related code, improve encapsulation, and make applications easier to maintain. The choice of nested class depends on how closely the inner class is associated with the outer class and where it needs to be accessed.
The four types of nested classes are:
- Static Nested Class
- Inner Class
- Local Inner Class
- Anonymous Inner Class

1. Static Nested Class
What is a Static Nested Class? A Static Nested Class is a class declared with the static keyword inside another class. Since it belongs to the outer class rather than to an object of the outer class, it can be created without creating an instance of the outer class.
A static nested class can directly access only the static members of the outer class. To access non-static members, it must use an object of the outer class.
When to Use Use a static nested class when the nested class is logically related to the outer class but does not depend on the state of its objects.
Characteristics
- Declared using the static keyword.
- Does not require an outer class object.
- Can access static members directly.
- Cannot directly access non-static members.
- Behaves similarly to a normal class but is logically grouped inside another class.
Advantages
- Improves code organization.
- Reduces unnecessary object creation.
- Suitable for utility or helper classes.
- Provides better encapsulation.
2. Inner Class
What is an Inner Class? An Inner Class is a non-static class declared inside another class. It is associated with an instance of the outer class, which means an outer class object must exist before creating an inner class object. An inner class has direct access to all members of the outer class, including private variables and methods.
When to Use Use an inner class when the nested class is closely associated with a specific object of the outer class.
Characteristics

- Does not use the static keyword.
- Requires an outer class object.
- Can access both static and non-static members.
- Can access private members of the outer class.
- Represents a strong relationship with the outer class.
Advantages
- Improves encapsulation.
- Keeps related classes together.
- Simplifies code organization.
- Allows direct access to outer class members.
3. Local Inner Class
What is a Local Inner Class?

A Local Inner Class is a class declared inside a method, constructor, or block of code. Its scope is limited to that block, meaning it cannot be accessed from outside the method in which it is declared. Once the method finishes execution, the local inner class is no longer accessible.
When to Use Use a local inner class when a helper class is needed only within a specific method and is not required elsewhere.
Characteristics
- Declared inside a method or constructor.
- Accessible only within that method.
- Cannot be accessed outside its scope.
- Can access local variables that are final or effectively final.
- Used for temporary or method-specific functionality.
Advantages
- Restricts visibility.
- Improves encapsulation.
- Reduces unnecessary classes.
- Keeps temporary logic localized.
4. Anonymous Inner Class
What is an Anonymous Inner Class? An Anonymous Inner Class is an inner class without a name. It is declared and instantiated at the same time and is typically used when a class or interface needs to be implemented only once. Since it has no name, it cannot be reused elsewhere in the program.
When to Use Use an anonymous inner class for one-time implementations, such as event handling, callbacks, or temporary object behavior.
Characteristics
- Does not have a class name.
- Declared and instantiated together.
- Used only once.
- Cannot have an explicit constructor.
- Can extend one class or implement one interface.
Advantages
- Reduces boilerplate code.
- Simplifies one-time implementations.
- Improves readability for small tasks.
- Commonly used in GUI and event-driven programming.
Advantages
- Better code organization.

A nested class groups a helper tightly with its enclosing class and can reach its private state.
public class LinkedList {
private Node head;
// Node only matters to LinkedList — nest it
private static class Node {
int value;
Node next;
Node(int value) { this.value = value; }
}
void addFirst(int value) {
Node n = new Node(value);
n.next = head;
head = n;
}
}