Overview of Nested, Static, Anonymous, Local Inner Classes


<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents

</aside>

<aside> 💡

  1. Inner Classes in Java </aside>

Inner Classes in Java


  1. Inner Classes in Java

    1. Inner classes are classes defined within another class. They are primarily used to logically group classes that are tightly bound to each other. Inner classes improve encapsulation and can access all the members (even private ones) of their enclosing class.

  2. Types of Inner Classes

    1. Nested Class:
      1. Includes both static and non-static inner classes.
      2. Declared as a member of an outer class.
    2. Static Nested Class:
      1. Declared as static.
      2. Does not have access to instance members of the outer class.
    3. Anonymous Inner Class:
      1. Defined without a name.
      2. Typically used for creating a one-time implementation of a class or interface.
    4. Local Inner Class:
      1. Defined within a method or block of code.
      2. Cannot be declared as static.

  3. Nested Inner Class

    1. A nested inner class is a non-static class defined within another class. It has access to all members of the enclosing class, including private members.

    2. Syntax:

      class OuterClass {
          class InnerClass {
              // Inner class code
          }
      }
      
    3. Example: Nested Inner Class

      class Outer {
          private String message = "Hello from Outer";
      
          class Inner {
              void displayMessage() {
                  System.out.println(message);
              }
          }
      }
      
      public class Main {
          public static void main(String[] args) {
              Outer outer = new Outer();
              Outer.Inner inner = outer.new Inner();
              inner.displayMessage();
          }
      }
      
      
    4. Output:

      Hello from Outer
      

  4. Static Nested Class

    1. A static nested class is declared using the static keyword. Unlike non-static inner classes, it does not require an instance of the outer class and can access only the static members of the outer class.

    2. Syntax:

      class OuterClass {
          static class StaticNestedClass {
              // Static nested class code
          }
      }
      
    3. Example: Static Nested Class

      class Outer {
          static String message = "Hello from Outer";
      
          static class StaticNested {
              void displayMessage() {
                  System.out.println(message);
              }
          }
      }
      
      public class Main {
          public static void main(String[] args) {
              Outer.StaticNested nested = new Outer.StaticNested();
              nested.displayMessage();
          }
      }
      
      
    4. Output:

      Hello from Outer
      

  5. Anonymous Inner Class

    1. An anonymous inner class is a class without a name. It is used to provide an implementation of an abstract class or interface. This type of inner class is created on-the-fly and used immediately.

    2. Syntax:

      interface Greet {
          void sayHello();
      }
      
    3. Example: Anonymous Inner Class

      interface Greet {
          void sayHello();
      }
      
      public class Main {
          public static void main(String[] args) {
              Greet greet = new Greet() { // Anonymous Inner Class
                  @Override
                  public void sayHello() {
                      System.out.println("Hello from Anonymous Inner Class");
                  }
              };
      
              greet.sayHello();
          }
      }
      
      
    4. Output:

      Hello from Anonymous Inner Class
      

  6. Local Inner Class

    1. A local inner class is defined inside a method or block and is scoped only within that method. It cannot be declared as static and can access final or effectively final variables of the enclosing scope.

    2. Syntax:

      class OuterClass {
          void method() {
              class LocalInnerClass {
                  // Local inner class code
              }
          }
      }
      
    3. Example: Local Inner Class

      class Outer {
          void display() {
              final String message = "Hello from Local Inner Class";
      
              class LocalInner {
                  void showMessage() {
                      System.out.println(message);
                  }
              }
      
              LocalInner localInner = new LocalInner();
              localInner.showMessage();
          }
      }
      
      public class Main {
          public static void main(String[] args) {
              Outer outer = new Outer();
              outer.display();
          }
      }
      
      
    4. Output:

      Hello from Local Inner Class
      

  7. Comparison of Inner Classes

    Type Access to Outer Class Members Static Allowed Usage Context
    Nested Inner Class Yes No When tight coupling with outer class is needed.
    Static Nested Class Only static members Yes When no association with an outer instance.
    Anonymous Inner Class Yes No For short-lived implementations.
    Local Inner Class Yes No Scoped to a specific method or block.