What Are Inner Classes?
You'll learn: What inner classes are and how they differ from static nested classes by maintaining a live connection to an instance of the outer class.
Core Definition
An inner class is a non-static nested class declared inside another class. Unlike static nested classes (which you just learned), inner classes have a special relationship with their outer class: every instance of an inner class is bound to a specific instance of the outer class.
Think of it like a personal assistant: while a static nested class is like a company-wide resource anyone can use, an inner class is like a personal assistant tied to one specific manager—it always knows which manager it works for and can access that manager's private information.
The Critical Difference
When you create an inner class object, it secretly holds a reference to the outer class instance that created it. This means:
- Inner classes can access all members of the outer class, including private fields and methods
- You must have an outer class instance first before you can create an inner class instance
- Each inner class object is permanently associated with one outer object
Why It Matters
This tight coupling makes inner classes perfect for helper objects that need intimate access to the outer object's state. For example, an iterator inside a collection class, or event handlers that need to manipulate the enclosing object's data.
The binding happens automatically—the inner class always "knows" which outer instance it belongs to, without you passing references around explicitly.
Key Takeaway: Inner classes are non-static nested classes that maintain a permanent, hidden reference to a specific instance of their outer class, giving them privileged access to that instance's private members.