Java Inner Classes

What are Inner Classes in Java?

An inner class in Java is a class defined within another class. Inner classes are associated with their enclosing class and provide a way to logically group classes that are used together. This structure improves code readability and makes the program modular.

Inner classes are used when a class is highly coupled to its enclosing class or when it logically belongs to the outer class.

Types of Inner Classes in Java

Java provides four types of inner classes:

  1. Member Inner Class
  2. Static Nested Class
  3. Local Inner Class
  4. Anonymous Inner Class

Each type serves a specific purpose and has unique features.

1. Member Inner Class

A member inner class is a non-static class defined directly within an enclosing class. It can access all members (both static and non-static) of the outer class, including private ones.

Example of Member Inner Class:

class Outer {
private String message = "Hello from Outer Class";

class Inner {
void displayMessage() {
System.out.println(message); // Accessing outer class member
}
}
}

public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // Create inner class instance
inner.displayMessage();
}
}

Output:

Hello from Outer Class

2. Static Nested Class

A static nested class is a static class defined within an enclosing class. Since it is static, it cannot access non-static members of the outer class directly. However, it can access static members.

Example of Static Nested Class:

class Outer {
static String staticMessage = "Static message from Outer Class";

static class StaticNested {
void displayStaticMessage() {
System.out.println(staticMessage); // Accessing static member
}
}
}

public class Main {
public static void main(String[] args) {
Outer.StaticNested nested = new Outer.StaticNested(); // Create static nested class instance
nested.displayStaticMessage();
}
}

Output:

Static message from Outer Class

3. Local Inner Class

A local inner class is defined inside a method or a block. It is local to that method or block, meaning it cannot be accessed outside it. Local inner classes can access final or effectively final variables of the method in which they are defined.

Example of Local Inner Class:

class Outer {
void display() {
final int number = 10; // Effectively final variable

class LocalInner {
void printNumber() {
System.out.println("Number: " + number);
}
}

LocalInner localInner = new LocalInner();
localInner.printNumber();
}
}

public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.display();
}
}

Output:

Number: 10

4. Anonymous Inner Class

An anonymous inner class does not have a name. It is used to provide an immediate implementation of an interface or to override the methods of a class.

Example of Anonymous Inner Class:

interface Greeting {
void sayHello();
}

public class Main {
public static void main(String[] args) {
Greeting greeting = new Greeting() { // Anonymous inner class
@Override
public void sayHello() {
System.out.println("Hello from Anonymous Inner Class");
}
};

greeting.sayHello();
}
}

Output:

Hello from Anonymous Inner Class

Advantages of Using Inner Classes

  1. Encapsulation: Inner classes logically group classes together, improving encapsulation.
  2. Code Readability: They make code more readable by keeping closely related classes together.
  3. Access to Outer Class Members: Inner classes can access private members of the outer class, simplifying logic.
  4. Reduced Complexity: Inner classes reduce the number of top-level classes, simplifying package organization.

When to Use Inner Classes

  • Use member inner classes when the inner class requires access to all members of the outer class.
  • Use static nested classes when the inner class does not require access to non-static members of the outer class.
  • Use local inner classes for small tasks where the class is only needed within a single method.
  • Use anonymous inner classes for short-lived implementations, such as event handling.

Leave a Comment