Inner Classes in Java
What is an Inner Class?
An inner class is a class defined within another class. It is associated with its enclosing class and can access all the members (including private members) of the outer class. Inner classes are primarily used to logically group classes, improve encapsulation, and make the code more readable.

Key Features of Inner Classes:
They can access private members of the outer class.
They are used to logically group classes that are only used in one place.
They increase encapsulation by hiding implementation details.
Types of Inner Classes
Java provides four types of inner classes:
Member Inner Class
Static Nested Class
Local Inner Class
Anonymous Inner Class
Let’s explore each type with examples.
1. Member Inner Class
A member inner class is a non-static class defined inside another class. It has access to all members of the outer class, including private members.
Example:
class Outer {
private int data=10;
class Inner {
void show() {
System.out.println("Data: "+ data);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner inner = new Outer().new Inner();
inner.show(); // Data: 10
}
}
2. Static Nested Class
A static nested class is a static class defined inside another class. Since it is static, it cannot access non-static members of the outer class directly.
Example:
class Outer {
static int data=30;
static class Nested {
void show() {
System.out.println("Data: "+ data);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Nested nested = new Outer.Nested();
nested.show(); // Data: 30
}
}
3. Local Inner Class
A local inner class is defined within a method or a block. It is local to the scope of the method and cannot be accessed outside it.
Example:
class Outer {
void display() {
class Local {
void msg() {
System.out.println("Local Inner Class");
}
}
Local local = new Local();
local.msg();
}
}
public class Main {
public static void main(String[] args) {
new Outer().display(); // Local Inner Class
}
}
4. Anonymous Inner Class
An anonymous inner class is a class without a name. It is used to create a one-time implementation of an interface or an abstract class.
Example:
abstract class Person {
abstract void eat();
}
public class Main {
public static void main(String[] args) {
Person p = new Person() {
void eat() {
System.out.println("Eating");
}
};
p.eat(); // Eating
}
}
Real-Time Use Cases of Inner Classes
Inner classes are widely used in real-world applications. Here are some common scenarios:
1. Event Handling in GUI Applications
In GUI frameworks like Swing, inner classes are often used to handle events such as button clicks.
Example:
import javax.swing.*;
import java.awt.event.*;
public class ButtonExample {
private JFrame frame;
private JButton button;
public ButtonExample() {
frame = new JFrame("Button Example");
button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvente) {
JOptionPane.showMessageDialog(frame, "Button Clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new ButtonExample();
}
}
2. Encapsulating Helper Classes
Inner classes are used to encapsulate helper classes that are only relevant to the outer class, improving modularity and readability.
3. Implementing Callbacks
Anonymous inner classes are commonly used to implement callbacks in asynchronous programming.
Advantages of Inner Classes
Logical Grouping: Inner classes group related classes together, making the code more readable.
Encapsulation: They can access private members of the outer class, improving encapsulation.
Event Handling: Simplifies event handling in GUI applications.
Reduced Boilerplate: Anonymous inner classes reduce the need for separate class files.
