Java Inner Classes

An inner class is also called a nested class. It is a class which declared inside the class or interface. It is one class which is a member of another class. An inner class is used to group classes and interfaces together, which make our code is more readable and maintainable. Inner class access the members of the outer class, also including methods and private data members.

Syntax of Inner class
		class OuterClass {
		    ...
		    class NestedClass {
		        ...
		    }
		}

There are two types of nested class non-static and static nested classes, and non-static nested classes called inner class.

Types of Inner class

There are four types of inner class:

  • Nested inner class
  • Method local inner class
  • Anonymous inner class
  • Static inner class
  1. Nested Inner class

    It can access any private instance variable of the outer class.

    Example:
    				class Outer { 
    				   // Simple nested inner class 
    				   class Inner { 
    				      public void show() { 
    				           System.out.println("It is a nested class method"); 
    				      } 
    				   } 
    				} 
    				class Main { 
    				   public static void main(String[] args) { 
    				       Outer.Inner in = new Outer().new Inner(); 
    				       in.show(); 
    				   } 
    				}
    
  2. Method local inner class

    Within a method of an outer class, inner class can be declared.

    Example:
    				class Outer { 
    				    void outerMethod() { 
    				        System.out.println("Declare inside outerMethod"); 
    				        // Inner class is local to outerMethod() 
    				        class Inner { 
    				            void innerMethod() { 
    				                System.out.println("Declare inside innerMethod"); 
    				            } 
    				        } 
    				        Inner y = new Inner(); 
    				        y.innerMethod(); 
    				    } 
    				} 
    				class MethodDemo { 
    				    public static void main(String[] args) { 
    				        Outer x = new Outer(); 
    				        x.outerMethod(); 
    				    } 
    				}
    
    Output:

    Declare inside outerMethod

    Declare inside innerMethod

  3. Anonymous inner class

    Its classes can be declared without any name. Its name can be decided by java compiler.

  4. Static inner class

    It can be associated with its outer class. It can access it without creating an object of the outer class.

    Example:
    				class OuterClass {
    				  int a = 20;
    
    				  static class InnerClass {
    				    int b = 10;
    				  }
    				}
    
    				public class Main {
    				  public static void main(String[] args) {
    				    OuterClass.InnerClass myInner = new OuterClass.InnerClass();
    				    System.out.println(myInner.b);
    				  }
    				}
    
    Output:

    10

Question: the difference between inner class and nested class in java.

An inner class is a part of the nested class, and non-static nested classes are called inner class.

Copyright 1999- Ducat Creative, All rights reserved.