WikiGalaxy

Personalize

Java Inner Classes: Introduction

What are Inner Classes?

In Java, an inner class is defined within the body of another class. Inner classes can access the members of the outer class, including private ones, and are used to logically group classes that are only used in one place.


public class OuterClass {
  private String message = "Hello from OuterClass";

  class InnerClass {
    public void display() {
      System.out.println(message);
    }
  }
}
    

Console Output:

Hello from OuterClass

Types of Inner Classes

Nested Inner Class

A nested inner class is a class declared inside another class. It can be static or non-static.


public class OuterClass {
  static class StaticNestedClass {
    public void display() {
      System.out.println("Inside Static Nested Class");
    }
  }
}
    

Console Output:

Inside Static Nested Class

Anonymous Inner Class

Anonymous Inner Class Usage

An anonymous inner class is a class without a name and is instantiated in place. It's often used in graphical user interface (GUI) applications.


abstract class Animal {
  abstract void sound();
}

public class Main {
  public static void main(String[] args) {
    Animal dog = new Animal() {
      void sound() {
        System.out.println("Bark");
      }
    };
    dog.sound();
  }
}
    

Console Output:

Bark

Local Inner Class

Defining Local Inner Classes

Local inner classes are defined within a method. They have access to the method's local variables that are declared final.


public class OuterClass {
  void display() {
    class LocalInner {
      void msg() {
        System.out.println("Inside Local Inner Class");
      }
    }
    LocalInner local = new LocalInner();
    local.msg();
  }
}
    

Console Output:

Inside Local Inner Class

Member Inner Class

Characteristics of Member Inner Classes

Member inner classes are defined at the member level of the outer class. They can access all members of the outer class, including private ones.


public class OuterClass {
  private String msg = "Hello from Member Inner Class";

  class MemberInner {
    void display() {
      System.out.println(msg);
    }
  }
}
    

Console Output:

Hello from Member Inner Class

Static Nested Class

Understanding Static Nested Classes

Static nested classes are similar to static members. They can access static data members of the outer class but cannot access non-static data members and methods.


public class OuterClass {
  static int data = 30;

  static class StaticNested {
    void msg() {
      System.out.println("Data: " + data);
    }
  }
}
    

Console Output:

Data: 30

Accessing Inner Classes

How to Access Inner Classes

To access an inner class, you need to create an instance of the outer class first. Then, create an instance of the inner class using the outer class instance.


public class OuterClass {
  class Inner {
    void msg() {
      System.out.println("Accessing Inner Class");
    }
  }

  public static void main(String[] args) {
    OuterClass outer = new OuterClass();
    OuterClass.Inner inner = outer.new Inner();
    inner.msg();
  }
}
    

Console Output:

Accessing Inner Class

Advantages of Inner Classes

Benefits of Using Inner Classes

Inner classes can make code more readable and maintainable by logically grouping classes together. They also have access to the outer class's private members.


// Example showing advantage of inner class
public class OuterClass {
  private String data = "Inner class access";

  class Inner {
    void display() {
      System.out.println(data);
    }
  }
}
    

Console Output:

Inner class access

Limitations of Inner Classes

Drawbacks of Inner Classes

Inner classes can make code more complex and harder to understand if overused. They also increase the size of the outer class and can lead to memory leaks if not handled properly.


// Example illustrating potential complexity
public class OuterClass {
  class Inner {
    void complexMethod() {
      System.out.println("Complexity can increase");
    }
  }
}
    

Console Output:

Complexity can increase

Practical Applications

Where to Use Inner Classes

Inner classes are useful in scenarios where a class is needed only in one place. They are commonly used in event handling and callback mechanisms.


// Example of inner class in event handling
public class Button {
  void click() {
    class ClickListener {
      void onClick() {
        System.out.println("Button clicked");
      }
    }
    ClickListener listener = new ClickListener();
    listener.onClick();
  }
}
    

Console Output:

Button clicked

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025