Nearby lessons
45 of 125Java - Inner Classes
- What an inner class is and why we need it
- The four types: member, static nested, local, anonymous
- How to create objects of inner classes
- Where anonymous inner classes are used in real projects
Inner Classes is a core concept of the Java language. This lesson explains What is an Inner Class?, The Four Types of Inner Classes and Member Inner Class with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is an Inner Class?
An inner class is simply a class declared inside another class. Java allows a class to live inside another class, just like a room can have a cupboard inside it.
Why do we need inner classes? The main reason: grouping classes that belong together. If class B is used only by class A, it is cleaner to keep B inside A. Inner classes also get direct access to the private members of the outer class.
The Four Types of Inner Classes
| Type | Where declared | Short description |
|---|---|---|
| Member (non-static) inner class | Inside the class, outside any method | Normal inner class. Needs an outer object to exist. |
| Static nested class | Inside the class with static keyword | Like a static member. Does not need an outer object. |
| Local inner class | Inside a method | Exists only inside that method. |
| Anonymous inner class | Inside a method or statement | A class with no name, used for one-time tasks. |
Member Inner Class
A member inner class behaves like a non-static member of the outer class. To create its object, you first need an object of the outer class.
Notice the inner class can directly use x (the outer class private member). The syntax outer.new Inner() tells Java: create this inner object inside that specific outer object.
Local Inner Class
A local inner class is declared inside a method. It is visible only inside that method.
When to Use Which Type — Trainer Advice
| Situation | Which inner class to use |
|---|---|
| The inner class always needs the outer object and shares its state | Member inner class |
| The inner class works independently, just grouped logically | Static nested class |
| Helper logic used in only one method | Local inner class |
| A one-time object with a small override/implementation | Anonymous inner class |
Interfaces and Classes Inside Each Other
The classic material asks two interesting questions about nesting between classes and interfaces:
Can we write an interface inside a class?
Yes. An interface can be declared inside a class. But the rule is: the implementation class of that inner interface must also be declared inside the same outer class.
Can we write a class inside an interface?
Yes. If you declare a class inside an interface, that class automatically becomes a static inner class — you access it through the interface name, with no outer object needed.
Interfaces and Classes Inside Each Other
You can also write an abstract class inside an interface — its sub class must then also be declared inside that same interface (it becomes a static inner sub class).
- Inner class = a class declared inside another class.
- Member inner class needs an outer object: outer.new Inner().
- Static nested class is created with Outer.Nested, no outer object needed.
- Local inner class lives inside a method.
- Anonymous inner class has no name and is used for one-time implementations.
- Modern code prefers lambda expressions for one-method interfaces, but anonymous classes still appear in real projects.