What is a Nested Class in OOP?
Learn what a nested class is in OOP -- static nested classes vs inner classes -- with Java examples and common interview questions.
Expected Interview Answer
A nested class is a class defined entirely within the body of another class, used to logically group classes that are only meaningful in the context of their enclosing class.
Nested classes come in two broad forms: static nested classes, which behave like a top-level class but are namespaced inside the enclosing class and do not need an outer instance, and non-static inner classes, which hold an implicit reference to an enclosing instance. Nesting improves encapsulation by hiding a helper class from the rest of the package when it is only useful alongside its outer class, and it improves readability by keeping tightly related types physically together. The term 'nested class’ is the umbrella category; 'inner class’ specifically refers to the non-static variety.
- Groups logically related classes together for readability
- Increases encapsulation by scoping helper classes to their owner
- Static nested classes avoid the overhead of an outer instance reference
- Keeps the enclosing class's package namespace cleaner
AI Mentor Explanation
A 'Scorecard' class might define a small 'Innings’ helper type entirely within itself, because an innings only ever makes sense as part of a specific scorecard. Nesting Innings inside Scorecard keeps that relationship explicit in the code’s structure, the same way a scorebook keeps each innings recorded on its own dedicated page inside the same book rather than as a loose separate document. That grouping is exactly what a nested class provides: a type that only matters in the context of its owner, defined right inside it.
Step-by-Step Explanation
Step 1
Choose static vs non-static
Decide if the nested class needs access to an outer instance; if not, make it static.
Step 2
Define it inside the outer class body
Write the class declaration physically within the enclosing class braces.
Step 3
Control visibility
Use private/protected/public to scope how much of the codebase can see the nested type.
Step 4
Reference it via the outer class name
External code refers to it as OuterClass.NestedClass.
What Interviewer Expects
- Distinction between static nested classes and non-static inner classes
- A rationale for nesting: encapsulation and logical grouping
- Correct syntax for referring to a nested class from outside
- Awareness that nested classes can access the outer class's private members
Common Mistakes
- Treating 'nested' and 'inner' as strictly identical (inner is a subset of nested)
- Making a nested class non-static when it never needs the outer instance, wasting a hidden reference
- Forgetting that a static nested class cannot access outer instance fields directly
- Overusing nesting for classes that are actually useful independently elsewhere
Best Answer (HR Friendly)
“A nested class is simply a class defined inside another class, used when a helper type only makes sense in the context of its owner. It keeps closely related code together and can be either static, meaning it stands alone, or non-static, meaning it’s tied to a specific instance of the outer class.”
Code Example
public class Inventory {
private List<Slot> slots = new ArrayList<>();
// Static nested class: does not need an outer Inventory instance
public static class Slot {
String itemName;
int quantity;
Slot(String itemName, int quantity) {
this.itemName = itemName;
this.quantity = quantity;
}
}
void addSlot(String itemName, int quantity) {
slots.add(new Slot(itemName, quantity));
}
}
// Referenced from outside as Inventory.Slot
Inventory.Slot slot = new Inventory.Slot("Sword", 1);Follow-up Questions
- What is the difference between a static nested class and an inner class?
- Why would you choose a nested class over a top-level class in its own file?
- Can a static nested class access the outer class's instance fields?
- What is a local class, and how does it relate to nested classes?
MCQ Practice
1. A static nested class differs from an inner class in that it?
A static nested class behaves like a top-level class namespaced inside the outer one, with no implicit outer-instance link.
2. How is a static nested class typically referenced from outside code?
External code accesses it via OuterClass.NestedClass, using the outer class as a namespace.
3. What is a key benefit of nesting a helper class inside another class?
Nesting hides a helper type that only makes sense alongside its owner, improving encapsulation and readability.
Flash Cards
Nested class in one line? — A class defined inside another class, grouping logically related types together.
Two main kinds? — Static nested classes and non-static inner classes.
Key difference? — Static nested classes have no implicit outer-instance reference; inner classes do.
How referenced externally? — As OuterClass.NestedClassName.