Java Inner Classes

 

In Java, you can define a class inside another class. These are called inner classes (or nested classes).

Inner classes are useful when you want to logically group classes together, improve encapsulation, or create helper classes that should not exist outside their parent class.
In this guide, we’ll cover:

  • What inner classes are
  • Types of inner classes in Java
  • Examples with code
  • Best practices

What Are Inner Classes?

 
  • A class declared inside another class.
  • Can access members (including private) of the outer class.
  • Improves code organization and encapsulation.

Types of Inner Classes

 

Java provides four types of inner classes:

  1. Non-static inner class (regular inner class)
  2. Static nested class
  3. Local inner class
  4. Anonymous inner class

1. Non-Static Inner Class

 
  • Associated with an instance of the outer class.
  • Can access all members (including private) of the outer class.

Example:

public class Outer {
  private String message = “Hello from Outer!”;

  public class Inner {
    public void display() {
      System.out.println(message); // access outer class variable
    }
  }

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

2. Static Nested Class

 
  • Declared as static inside another class.
  • Does not require an instance of the outer class.
  • Can only access static members of the outer class.

Example:

public class OuterStatic {
  private static String msg = “Hello from Static Nested Class!”;

  public static class Nested {
    public void display() {
      System.out.println(msg);
    }
  }

  public static void main(String[] args) {
    OuterStatic.Nested nested = new OuterStatic.Nested();
    nested.display();
  }
}

3. Local Inner Class

 
  • Defined inside a method or block.
  • Scope is limited to that method/block.

Example:

public class OuterLocal {
  public void show() {
    class Local {
      void print() {
        System.out.println(“Hello from Local Inner Class!”);
      }
    }

    Local local = new Local();
    local.print();
  }

  public static void main(String[] args) {
    OuterLocal obj = new OuterLocal();
    obj.show();
  }
}

4. Anonymous Inner Class

 
  • A class without a name, created for one-time use.
  • Commonly used for implementing interfaces or abstract classes on the fly.

Example:

interface Greeting {
  void sayHello();
}

public class AnonymousExample {
  public static void main(String[] args) {
    Greeting g = new Greeting() {
      @Override
      public void sayHello() {
        System.out.println(“Hello from Anonymous Inner Class!”);
      }
    };
    g.sayHello();
  }
}

When to Use Inner Classes

 
  • Non-static inner classes → when you need access to outer class instance.
  • Static nested classes → when grouping helper classes logically.
  • Local classes → for temporary logic inside a method.
  • Anonymous classes → for quick, one-time implementations (event listeners, callbacks).

Best Practices

 
  • Use inner classes only when they improve readability and structure.
  • Prefer static nested classes if the class doesn’t depend on an outer instance.
  • Keep local and anonymous classes short.
  • For Java 8+, prefer lambdas over anonymous classes for functional interfaces.

Conclusion

 

Java Inner Classes provide a powerful way to structure your code, encapsulate logic, and create temporary implementations.

  • Use inner classes for object-level relationships.
  • Use static nested classes for grouping helper classes.
  • Use local classes when you need logic inside a method.
  • Use anonymous classes for one-time behavior.

By mastering inner classes, you’ll write cleaner, more modular, and more maintainable Java programs.

Scroll to Top