-
Hajipur, Bihar, 844101
In Java, an inner class is a class defined inside another class. It allows grouping logically related classes together and helps make the outer class more readable and organized. Inner classes are mainly used when a class is useful only within another class.
They provide a way to logically associate two classes that are closely connected. Because the inner class can access members of its outer class (including private ones), it offers better encapsulation and code organization.
Inner classes are not just a structural choice — they help make code modular and cleaner. Some common reasons to use them are:
To logically group classes that belong together.
To make the code more readable and maintainable.
To access private members of the outer class directly.
To hide one class from outside access when it’s meant for internal use only.
For example, when creating a Car class, you can define an inner class Engine inside it because the engine belongs specifically to the car and is not used separately.
There are four main types of inner classes in Java:
Non-static inner class (Member inner class)
Static nested class
Local inner class (inside a method)
Anonymous inner class
Let’s understand each one with examples.
A non-static inner class is associated with an instance of its outer class. That means you need to create an object of the outer class before you can create an inner class object.
Example:
class Car {
private String brand = "Honda";
class Engine {
void showBrand() {
System.out.println("Car brand is: " + brand);
}
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
Car.Engine engine = car.new Engine();
engine.showBrand();
}
}
Explanation:
Here, Engine is an inner class inside Car. It can directly access the private field brand of the outer class. You create it using car.new Engine() because it depends on a Car object.
A static nested class does not require an instance of the outer class. It can only access the static members of the outer class directly.
Example:
class Car {
static String type = "Sedan";
static class Details {
void displayType() {
System.out.println("Car type: " + type);
}
}
}
public class Main {
public static void main(String[] args) {
Car.Details detail = new Car.Details();
detail.displayType();
}
}
Explanation:
Because the Details class is static, it can be accessed without creating an object of Car. You call it directly using Car.Details.
A local inner class is defined inside a method of the outer class. It is created and used only within that method.
Example:
class Car {
void start() {
class Engine {
void run() {
System.out.println("Engine is running");
}
}
Engine e = new Engine();
e.run();
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Explanation:
The inner class Engine is declared inside the start() method and can only be used within that method. It helps create small helper classes that don’t need to exist outside.
An anonymous inner class is a class without a name. It is usually used to provide an immediate implementation of an interface or abstract class.
Example:
abstract class Vehicle {
abstract void move();
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Vehicle() {
void move() {
System.out.println("Vehicle is moving");
}
};
v.move();
}
}
Explanation:
Here, the anonymous inner class provides the implementation for the abstract method move() directly, without creating a separate named subclass. This makes code shorter when a class is needed only once.
An inner class can access the outer class members using the syntax OuterClassName.this.memberName. This is helpful when there are naming conflicts between outer and inner class variables.
Example:
class Outer {
int num = 10;
class Inner {
int num = 20;
void display() {
System.out.println("Inner num: " + num);
System.out.println("Outer num: " + Outer.this.num);
}
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}
Output:
Inner num: 20
Outer num: 10
Inner classes make code more readable by logically grouping related code.
They improve encapsulation, as the inner class can be hidden from other classes.
Inner classes have direct access to all members of the outer class.
Anonymous classes make it easier to implement interfaces quickly.
| Feature | Inner Class | Static Nested Class |
|---|---|---|
| Requires outer object | Yes | No |
| Can access static members of outer class | Yes | Yes |
| Can access non-static members of outer class | Yes | No |
| Associated with | Instance | Class |
| Object creation | outer.new Inner() |
Outer.Nested() |
Event handling in GUI frameworks like Swing.
Encapsulating small helper classes within the main class.
Simplifying one-time implementations using anonymous inner classes.
Organizing large classes into smaller, more focused units.
Inner classes in Java make the code more structured and encapsulated. They allow defining a class within another class, giving the inner class access to all outer class members. The four types — member, static nested, local, and anonymous — serve different purposes but all aim to keep related code together. Mastering inner classes helps write cleaner and more maintainable Java programs, especially in GUI applications and large-scale systems.
Create a class Car with a non-static inner class Engine. The Engine class should have a method start() that prints “Engine started”. Create objects properly and call the method.
Write a Java program where an outer class Library contains an inner class Book. The inner class should display book details and access private members of the outer class.
Create a static nested class Details inside a class Mobile. The nested class should print the mobile brand name. Access it without creating an object of the outer class.
Define a class Bank that has a local inner class Account inside a method createAccount(). Use the local inner class to display account details.
Write a Java program that demonstrates the use of Outer.this.memberName to access an outer class variable when both outer and inner classes have variables with the same name.
Create an abstract class Shape with an abstract method area(). Inside the main method, define an anonymous inner class that implements area() and prints “Area calculated”.
Develop a class University that contains a non-static inner class Department. The inner class should display the university and department names.
Create a class Computer with a static nested class Hardware. The nested class should display the processor name. Demonstrate how to access it from the main class.
Write a Java program with a method display() inside a class Outer. Define a local inner class inside it named Message that prints “Welcome to Inner Classes”.
Create an interface Playable with a method play(). Implement it using an anonymous inner class that prints “Playing game” in the main method.