Inner classes

Java 9 min min read Updated: Mar 31, 2026 Intermediate
Inner classes
Intermediate Topic 11 of 14

Inner Classes

In Java, it is possible to define one class inside another class. Such a class is called an inner class or nested class, depending on its type. Inner classes are useful when one class is strongly connected to another and is needed only within that outer class.

Inner classes help improve code grouping, encapsulation, and logical organization. They are commonly used in event handling, callbacks, framework code, and situations where a helper class is tightly related to one outer class.

Key Concept: An inner class is a class defined inside another class. It allows better organization and direct access to outer class members.

What is an Inner Class?

An inner class is a class declared inside another class. The class that contains it is called the outer class, and the class inside it is called the inner class.

Basic Example

java class Outer { class Inner { void show() { System.out.println("Inside Inner Class"); } } }

Here:

  • Outer is the outer class
  • Inner is the inner class

Why Inner Classes are Used

  • to logically group classes that are used only in one place
  • to improve encapsulation
  • to access outer class members directly
  • to write cleaner event-handling or callback code

Types of Inner Classes in Java

Java mainly supports the following types of inner/nested classes:

  • Member Inner Class
  • Static Nested Class
  • Local Inner Class
  • Anonymous Inner Class

1. Member Inner Class

A member inner class is a non-static class declared directly inside another class, outside any method.

It behaves like an instance member of the outer class.

Example

java class Outer { int x = 10; class Inner { void display() { System.out.println("x = " + x); } } } public class Main { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.display(); } }

Explanation

  • Inner can directly access outer class variable x
  • To create inner class object, an outer class object is required

How to Create Member Inner Class Object

Syntax:

java Outer outerObj = new Outer(); Outer.Inner innerObj = outerObj.new Inner();

This is needed because the member inner class belongs to an object of the outer class.

Accessing Private Members of Outer Class

One powerful feature of inner classes is that they can access even private members of the outer class.

java class Outer { private int x = 100; class Inner { void show() { System.out.println("Private x = " + x); } } }

This is allowed because inner classes are considered closely connected to the outer class.

2. Static Nested Class

A static nested class is declared with the static keyword inside the outer class.

Although it is inside another class, it behaves more like a static member than a normal inner class.

Example

java class Outer { static int x = 50; static class Inner { void display() { System.out.println("x = " + x); } } } public class Main { public static void main(String[] args) { Outer.Inner i = new Outer.Inner(); i.display(); } }

Key Difference

  • Static nested class does not require outer class object
  • It can directly access only static members of the outer class

Member Inner Class vs Static Nested Class

Point Member Inner Class Static Nested Class
Static? No Yes
Needs outer object? Yes No
Can access outer instance members directly? Yes No
Can access outer static members directly? Yes Yes

3. Local Inner Class

A local inner class is declared inside a method, constructor, or block. Its scope is limited to that method or block only.

Example

java class Outer { void show() { class LocalInner { void display() { System.out.println("Inside local inner class"); } } LocalInner obj = new LocalInner(); obj.display(); } } public class Main { public static void main(String[] args) { Outer o = new Outer(); o.show(); } }

Here, LocalInner exists only inside the show() method.

Features of Local Inner Class

  • declared inside a method or block
  • accessible only within that method/block
  • can access outer class members
  • can access effectively final local variables

Accessing Local Variables in Local Inner Class

A local inner class can access local variables of the enclosing method only if they are final or effectively final.

java class Outer { void show() { int num = 20; class LocalInner { void display() { System.out.println("num = " + num); } } LocalInner obj = new LocalInner(); obj.display(); } }

If num is modified later, the compiler will not allow access inside the local inner class.

4. Anonymous Inner Class

An anonymous inner class is a class without a name. It is created and used in a single expression.

It is often used when:

  • the class is needed only once
  • you want a quick implementation of an interface or abstract class

Example with Interface

java interface Greeting { void sayHello(); } public class Main { public static void main(String[] args) { Greeting g = new Greeting() { public void sayHello() { System.out.println("Hello from anonymous inner class"); } }; g.sayHello(); } }

Here:

  • no class name is given
  • the implementation is created instantly

Anonymous Inner Class with Abstract Class

java abstract class Animal { abstract void sound(); } public class Main { public static void main(String[] args) { Animal a = new Animal() { void sound() { System.out.println("Dog barks"); } }; a.sound(); } }

This is useful for quick, one-time behavior definitions.

Where Inner Classes are Commonly Used

  • event handling in GUI programming
  • callback implementations
  • thread/task implementation
  • helper classes tightly coupled with outer class
  • framework and listener-based programming

Inner Class and Encapsulation

Inner classes improve encapsulation by hiding helper logic inside the outer class instead of exposing separate top-level classes.

Example:

  • a BankAccount class may contain a helper class for transaction validation
  • a GUI component may have an internal event handler class

Inner Class and Outer Class Relationship

A non-static inner class object always keeps a hidden reference to its outer class object. That is why it can directly access outer class members.

A static nested class does not keep this reference.

Real-World Example

Suppose a university portal has a Student class and inside it, an Address helper class that is meaningful only in the context of a student.

java class Student { String name = "Amit"; class Address { String city = "Delhi"; void showAddress() { System.out.println(name + " lives in " + city); } } } public class Main { public static void main(String[] args) { Student s = new Student(); Student.Address a = s.new Address(); a.showAddress(); } }

This shows logical grouping of related data.

Inner Class vs Normal Class

Point Inner Class Normal Class
Declared inside another class? Yes No
Logical grouping Stronger Independent
Access to outer members Directly possible Not direct
Use case Closely related helper class Independent entity

Common Mistakes

  • confusing member inner class with static nested class
  • trying to create member inner class object without outer object
  • assuming local inner class can be accessed outside its method
  • using anonymous inner classes for very complex logic
  • forgetting effectively final rule for local variables

Best Practices

  • use inner classes only when there is a strong relationship with outer class
  • prefer static nested classes when outer object access is not required
  • keep anonymous inner classes small and focused
  • avoid unnecessary nesting that hurts readability
  • use clear naming and structure for maintainability

Interview-Oriented Points

  • Inner class is a class declared inside another class
  • Types are member inner class, static nested class, local inner class, and anonymous inner class
  • Member inner class requires outer class object
  • Static nested class does not require outer object
  • Inner classes can access private members of outer class
  • Local inner class is valid only inside the method/block where it is declared
  • Anonymous inner class has no explicit class name and is used for one-time implementations

Conclusion

Inner classes are a useful feature in Java that allow tighter grouping of related classes and better encapsulation. They are especially helpful when one class is meaningful only inside another class.

Understanding all types of inner classes—member, static nested, local, and anonymous—helps a developer write cleaner and more structured Java code, especially in real-world event-driven and framework-based programming.

Quick Summary: Inner classes are classes declared inside another class, and Java supports member inner classes, static nested classes, local inner classes, and anonymous inner classes for different design needs.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators