Nearby lessons

45 of 125

Java - Inner Classes

📌 What You Will Learn
  • 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.

Example01
JCode Cell
1class Outer { // outer class
2 // ... members ...
3 
4 class Inner { // inner (nested) class
5 // ...
6 }
7}

The Four Types of Inner Classes

TypeWhere declaredShort description
Member (non-static) inner classInside the class, outside any methodNormal inner class. Needs an outer object to exist.
Static nested classInside the class with static keywordLike a static member. Does not need an outer object.
Local inner classInside a methodExists only inside that method.
Anonymous inner classInside a method or statementA 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.

In simple words: A member inner class needs an outer object to exist. You create it with outer.new Inner(), and it can directly access even the private members of the outer class.
Example03
JCode Cell
1class Outer {
2 int x = 100;
3 
4 class Inner {
5 void show() {
6 System.out.println("Inside inner class, x = " + x);
7 }
8 }
9}
10 
11class Test {
12 public static void main(String[] args) {
13 Outer outer = new Outer();
14 Outer.Inner inner = outer.new Inner(); // create inner using outer object
15 inner.show();
16 }
17}
Output
Inside inner class, x = 100

Local Inner Class

A local inner class is declared inside a method. It is visible only inside that method.

Example04
JCode Cell
1class Outer {
2 void display() {
3 class Local {
4 void msg() {
5 System.out.println("Local class inside method");
6 }
7 }
8 Local l = new Local(); // created and used only here
9 l.msg();
10 }
11}
12 
13class Test {
14 public static void main(String[] args) {
15 new Outer().display();
16 }
17}
Output
Local class inside method

When to Use Which Type — Trainer Advice

SituationWhich inner class to use
The inner class always needs the outer object and shares its stateMember inner class
The inner class works independently, just grouped logicallyStatic nested class
Helper logic used in only one methodLocal inner class
A one-time object with a small override/implementationAnonymous 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.

Example06
JCode Cell
1class Outer {
2 interface InnerI { // interface inside a class
3 void m1();
4 }
5 class Impl implements InnerI { // impl must be inside the same outer class
6 public void m1() { System.out.println("Inner interface implemented"); }
7 }
8}
9 
10class Test {
11 public static void main(String[] args) {
12 Outer o = new Outer();
13 Outer.InnerI i = o.new Impl();
14 i.m1();
15 }
16}
Output
Inner interface implemented

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).

Example07
JCode Cell
1interface I {
2 class A { // becomes a static inner class
3 void m1() { System.out.println("Class inside interface"); }
4 }
5}
6 
7class Test {
8 public static void main(String[] args) {
9 I.A a = new I.A(); // accessed like a static inner class
10 a.m1();
11 }
12}
Output
Class inside interface
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2