In Java, Inner Class also called as Nested Class is the class declared inside class or interface used to logically group classes and interfaces in one place to be more readable and maintainable.

Syntax of Inner class

class OuterClass{  
 class InnerClass{  
 }  
} 

Advantage of Java Inner Class

  • Nested Class can access all the members (data members and methods) of the outer class, including private.
  • Nested classes logically group classes and interfaces in one place to be more readable and maintainable
  • Nested Class is used for code optimization as it requires less code to write.

Difference between nested class and inner class in Java

An inner class is a part of a nested class. Non-static nested classes are known as inner classes.

Types of Nested Classes

There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.

Non-static nested class (Inner Class)

  1. Nested Inner Class
  2. Method Local Inner Class
  3. Anonymous Inner Class

4. Static Nested Class

Nested Inner Class :

Nested Inner Class can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier. Like class, an interface can also be nested and can have access specifiers.

Program : Java Program For Nested Class

class OuterCl_1 {

    class InnerCl_1 {

        public void display()
        {
            System.out.println("Hello From Nested Class Method");
        }
    }
}

public class InnerMain_1 {

	 public static void main(String[] args)
	    {
	        OuterCl_1.InnerCl_1 outerObj = new OuterCl_1().new InnerCl_1();
	        outerObj.display();
	    }
}

Output :

Hello From Nested Class Method

Method Local Inner Class :

When an Inner class is declared within a method of an outer class, we call it as Method Local Inner Class

Program : Java Program For Method Local Inner Class

class OuterCl_2 {
   void outerDisplay() {
      int x = 90;
      System.out.println("Hello From Outclass Method");
	  
      class InnerCl_2 {
         void innerDisplay() {
            System.out.println("Output From Inner Class Method x= "+x);
         }
      }
      InnerCl_2 objInner = new InnerCl_2();
      objInner.innerDisplay();
   }
}

class MethodLocalInnerClass {
   public static void main(String[] args) {
      OuterCl_2 outerObj=new OuterCl_2();
      outerObj.outerDisplay();
   }
}

Output :

Hello From Outclass Method
Output From Inner Class Method x= 90

Anonymous Inner Classes :

Inner class which is declared without any name is classed Anonymous Inner Class. There are two ways to declare Anonymous Inner Class.

  1. As a subclass of the specified type
  2. As an implementer of the specified interface

Program : Java Program For Anonymous Inner Class as a subclass of the specified type

class OuterCl_3 {

    void display()
    {
        System.out.println("Hello From The Outer Class");
    }
}

class InnerCl_3 {

    static OuterCl_3 outerObj = new OuterCl_3() {
        void display()
        {
            super.display();
            System.out.println("Hello From The Anonymous  Inner Class");
        }
    };

    public static void main(String[] args)
    {
        outerObj.display();
    }
}

Output :

Hello From The Outer Class
Hello From The Anonymous Inner Class

Program : Java Program For Anonymous Inner Class as an implementer of the specified interface

interface OuterCla {
    void display();
}

class InnerCl_4 {

    static OuterCla outerObj = new OuterCla() {
      
        public void display()
        {
            System.out.println("Hello From The Anonymous Inner Class");
        }
    };

    public static void main(String[] args)
    {
        outerObj.display();
    }
}

Output :

Hello From The Anonymous Inner Class

Program : Java Program For Anonymous Inner Class

abstract class AnonymousInner {
   public abstract void innerMethod();
}

public class Outer_class {

   public static void main(String args[]) {
      AnonymousInner objInner = new AnonymousInner() {
         public void innerMethod() {
            System.out.println("Hello From Anonymous Inner Class");
         }
      };
      objInner.innerMethod();	
   }
}

Output :

Hello From Anonymous Inner Class

Static Nested Class :

Static nested classes are not technically inner classes. They are like a static member of outer class by which you can access it without creating an object of the outer class

Program : Java Program For Static Nested Class

public class OuterCl_5 {

    private static void outerClMethod()
    {
        System.out.println("Hello From Outer Class Method");
    }


   static class NestedCl {
      public static void nestedMethod() {
         System.out.println("Hello From Static Nested Class");
		 outerClMethod();
      }
   }
   
   public static void main(String args[]) {
      OuterCl_5.NestedCl objNested = new OuterCl_5.NestedCl();	 
      objNested.nestedMethod();
	          
	  System.out.println("\n"); 

	  OuterCl.NestedCl.nestedMethod();

   }
}

Output :

Hello From Static Nested Class
Hello From Outer Class Method


Hello From Static Nested Class
Hello From Outer Class Method
Scroll to Top