Java Inner Class

Java allows nested classes so that programmer can write a class within a class. Other than methods and variables as class members, a class can also contain classes as its members.

Nested Classes

The class inside another class is a Java Inner Class. Inner classes can have private and protected access types, unlike regular classes. While the class that contains a class is called Java Outer class. The syntax of declaring a class inside a class is:

Access Java Inner Class

In order to access inner class of Java, user has to first create an object of outer class and then create an object of inner class. Here is a simple example that demonstrates the concept of nested classes. The code contains two classes an outer class and an inner class. Each class has its own variables a and b respectively.

// filename is Example.java
class OuterClass {  // Outer class
  int a = 15;

  class InnerClass {   // Inner Class
    int b = 35;
  }
}

public class Example {
  public static void main(String[] args) {
    OuterClass outerObj = new OuterClass();   // object of outer class
    OuterClass.InnerClass innerObj = outerObj.new InnerClass();   // object of inner class
    System.out.println(innerObj.b + outerObj.a);
  }
}

The outer class object accesses the variable of outer class and inner class object accesses inner class variable. The output of the above code after compilation is:

Types of Java Inner Classes

There are four basic types of Java inner classes:

  • Nested Inner Class
  • Method Local Inner Class
  • Anonymous Inner Class
  • Static Nested Class

Nested Inner Class

A nested inner class is able to access any private instance variable of outer class. The example shows the concept of Nested Inner Class.

// filename is Example.java
class OuterClass {   // Outer class
   class InnerClass {  // Nested Inner Class
      public void innerClassMethod() {
           System.out.println("This is a nested class method.");
      }
   }
}
class Example {
   public static void main(String[] args) {
       OuterClass.InnerClass obj = new OuterClass().new InnerClass();
       obj.innerClassMethod();
   }
}

The output of the above code after compilation is:

Method Local Inner Class

A class that is declared inside the method of an outer class is called method local inner class. The below example shows how to use method local inner class in Java program.

class OuterClass {
    void outerClassMethod() {
        System.out.println("This is outer class method");
        // Inner class is local to outerClassMethod()
        class InnerClass {
            void innerClassMethod() {
                System.out.println("This is inner class method");
            }
        }
        InnerClass obj1 = new InnerClass();
        obj1.innerClassMethod();
    }
}
public class Main {
    public static void main(String[] args) {
        OuterClass obj2 = new OuterClass();
        obj2.outerClassMethod();
    }
}

The outerClassMethod() calls the innerClassMethod() of Method Local Inner Class in its definition. In the Example class we only create object of the outer class. The outer class object calls the outer class method. The output of the above code after compilation is:

Anonymous Inner Class

Anonymous classes are nameless classes. They are declared without a name or identifier. The anonymous inner class is declared and instantiated at the same time. The below example shows the concept of anonymous inner class:

// File name is OuterClass.java
abstract class AnonymousInnerClass {
   public abstract void classmethod();
}

public class OuterClass {

   public static void main(String args[]) {
      AnonymousInnerClass obj = new AnonymousInnerClass() {
         public void classmethod() {
            System.out.println("This program gives an example of anonymous inner class");
         }
      };
      obj.classmethod();	
   }
}

The result of the above class after compilation and execution is:

Static Nested Class

A static nested class is a static member of outer class. The below example shows how to create a Static Nested Class:

// File name is InnerClass.java
class OuterClass {
   private static void outerClassMethod() {
     System.out.println("This is outer class Method");
   }
     
   // A static inner class
   static class InnerClass {
     public static void main(String[] args) {
        System.out.println("This is inner class Method");
        outerClassMethod();
     }
   }
 } 

The result of the above code after compilation and execution is:

Advantages of Java Inner Class

The purpose of creating Java Inner class is to group together the classes having similar functionality. It increases code readability and simplicity. It also becomes easier to maintain nested classes. Unlike regular classes inner class can be private which means it cannot be accessed from outside the class.