What is an Inner Class in OOP?
Learn what an inner class is in OOP -- how it binds to an outer instance and accesses private members -- with Java examples.
Expected Interview Answer
An inner class is a non-static nested class that holds an implicit reference to an instance of its enclosing class, giving it direct access to that instance’s fields and methods, even private ones.
Unlike a static nested class, an inner class cannot exist without an enclosing outer instance -- you create one via outerInstance.new InnerClass() in Java, or implicitly when instantiated from within an outer method. This tight coupling makes inner classes ideal for representing a component that is conceptually part of a specific outer object, such as an Iterator belonging to a particular Collection instance, where the inner class needs live access to the outer object’s internal state. The tradeoff is that each inner-class instance carries a hidden reference to its outer instance, which can prevent the outer object from being garbage collected while the inner instance is alive, and adds a small memory overhead per instance.
- Direct access to the enclosing instance's private members without extra plumbing
- Naturally models a component instance owned by a specific outer instance
- Keeps tightly coupled helper logic physically close to what it serves
- Enables patterns like custom Iterators tied to a specific collection instance
AI Mentor Explanation
A specific match’s 'Umpire' role only exists tied to that particular match instance -- it references and can query live details of that exact match’s state, like the current over and score. That live, instance-bound reference is exactly what an inner class provides: it isn’t a general umpire template, it’s bound to one specific match object and can reach into its current state directly. You can’t have an umpire-for-this-match without the match itself existing first.
Step-by-Step Explanation
Step 1
Declare a non-static nested class
Omit the static keyword so the class is tied to instances of the outer class.
Step 2
Access outer members directly
Code inside the inner class can reference outer fields and methods without qualification.
Step 3
Instantiate via an outer instance
Create it as outerInstance.new InnerClass(), or implicitly from within an outer method.
Step 4
Mind the lifecycle coupling
The inner instance holds a hidden reference to its outer instance, affecting garbage collection.
What Interviewer Expects
- Correct definition: non-static, tied to a specific outer instance
- Correct instantiation syntax and awareness of the implicit outer reference
- A realistic use case, e.g. a custom Iterator bound to a Collection instance
- Awareness of the memory/GC implication of the hidden outer reference
Common Mistakes
- Confusing an inner class with a static nested class
- Trying to instantiate an inner class without an enclosing instance
- Not realizing the inner class instance keeps its outer instance reachable, risking memory leaks
- Assuming inner classes can only be defined in one class per file
Best Answer (HR Friendly)
“An inner class is a non-static class defined inside another class that is tied to a specific instance of that outer class, and it can directly reach into that instance’s private fields. It’s useful when a helper object genuinely represents a piece of one particular outer object, like an iterator that belongs to one specific collection instance.”
Code Example
public class Playlist {
private List<String> tracks = new ArrayList<>();
void add(String track) { tracks.add(track); }
// Non-static inner class: bound to a specific Playlist instance
public class TrackIterator {
private int index = 0;
boolean hasNext() { return index < tracks.size(); }
String next() { return tracks.get(index++); } // reads outer’s private field
}
TrackIterator iterator() {
return new TrackIterator(); // implicit outer reference: this
}
}
Playlist p = new Playlist();
p.add("Song A");
Playlist.TrackIterator it = p.iterator();Follow-up Questions
- How do you instantiate an inner class from outside the outer class?
- What is the memory implication of an inner class holding a reference to its outer instance?
- How does an inner class differ from a static nested class in terms of access?
- What is a local inner class and when might you use one?
MCQ Practice
1. An inner class instance requires what to exist?
A non-static inner class always holds an implicit reference to a specific outer instance and cannot exist without one.
2. From outside the outer class, how do you create an inner class instance in Java?
Java requires the outerInstance.new InnerClass() syntax to bind the new inner instance to a specific outer instance.
3. A risk of overusing inner classes with long-lived references is?
Because each inner instance holds a reference to its outer instance, a long-lived inner object can keep the outer object alive unintentionally.
Flash Cards
Inner class in one line? — A non-static nested class bound to a specific enclosing instance, with direct access to its private members.
How is it instantiated externally? — outerInstance.new InnerClass()
Key risk? — It holds a hidden reference to its outer instance, which can delay garbage collection.
Classic use case? — A custom Iterator tied to one specific Collection instance.