Nested classes in java

Let’s learn nested classes in java.

Nested classes in java

A class inside another class is known as nested class (inner class). A nested class can be private.

Syntax:

class A // outer class
{
   ......
   class B // nested class
   {
      .....
   }
}


Nested classes in java are divided into two, they are –

  • static nested classes.
  • non-static nested classes (inner classes).
nested classes in java

static nested classes java

static inner class in java is static member of outer class.

static inner class in java can be accessed without creating object of outer class. It can accessed using other static members.

Static nested class java syntax:

class OuterClass 
{
   static class StaticNestedClass
   {
   }
}
OuterClass.StaticNestedClass obj = new OuterClass.StaticNestedClass();

Here’s an example on static nested classes java.

import java.util.*;
public class OuterClass
{
   static class NestedClass
   {
      public void display() 
      {
         System.out.println("static nested class example");
      }
   }
   public static void main(String[] args) 
   {
      OuterClass.NestedClass nested = new OuterClass.NestedClass();	 
      nested.display();
   }
}


Output:

static nested class example


Non-static nested class or Inner class

Non-static nested classes or an inner class is a class which is member of outer class.

Also read – inheritance in java

A class in java cannot be private. But a inner class is a member of another class can be private. Hence can access private members of a class.

There are four types of inner classes in java.

  • Inner class
  • Method local inner class
  • Anonymous inner classes

Inner class: inner class is a class created within a class. Inner class cannot be accessed from object of outer class.

Let’s see an example on inner class in java.

class OuterDemo
{
   int num;
   // inner class
   private class InnerDemo
   {
      public void display()
      {
         System.out.println("inner class");
      }
   }
   // access inner class from method within
   void printInner()
   {
      InnerDemo objInner = new InnerDemo();
      objInner.display();
   }
}
public class InnerClassExample
{
   public static void main(String[] args)
   {
      OuterDemo objOuter = new OuterDemo();
      objOuter.printInner();
   }
}


Output:

inner class


To access private members

Inner classes are used to access private members of a class. Here’s how to instantiate inner class.

OuterClass objOuter = new OuterClass(); 
OuterClass.InnerClass objInner = OuterClass.InnerClass();

Let’s see an example on how to access private members of a class.

class OuterClass
{
   // outer class private variable
   private int number = 56;
   // inner class
   public class InnerClass
   {
      public int showNumber()
      {
         System.out.println("showNumber() method of inner class.");
         return number;
      }
   }
}
public class AccessPrivateMembers
{
   public static void main(String[] args)
   {
      OuterClass objOuter = new OuterClass();
      OuterClass.InnerClass objInner = objOuter.new InnerClass();
      System.out.println(objInner.showNumber());
   }
}


Output:

showNumber() method of inner class.
56


Also read – learn static keyword in java


Method local inner classes

A method local inner class is class within a method. Scope of method local inner class is limited within the method.

Method local inner class in java can be instantiated within the method.

Let’s see an example on method local inner class in java.

class Outer
{
   // outer class instance method
   void display()
   {
      int num = 96;
      // method-local inner class
      class MethodLocalInner
      {
         public void show()
         {
            System.out.println("in method local inner class " + num);
         }
      } // end of inner class
      // access method local inner class
      MethodLocalInner objInner = new MethodLocalInner();
      objInner.show();
   }
   public static void main(String[] args)
   {
      Outer objOuter = new Outer();
      objOuter.display();
   }
}


Output:

in method local inner class 96


Anonymous inner class in java

Anonymous inner class is an inner class declared without class name.

Anonymous inner class are used to override method of an interface or a class.

Anonymous inner classes are declared and instantiated at the same time. Here’s the syntax.

AnonymousInner obj = new AnonymousInner() {
public void demoMethod() {

}
};

Anonymous inner class example: First let’s see an example on how to override method of a class.

abstract class Anonymous
{
   public abstract void display();
}
public class AnonymousInnerExample
{
   public static void main(String[] args)
   {
      Anonymous obj = new Anonymous() {
         public void display() {
            System.out.println("anonymous inner class example");
         }
      };
      obj.display();
   }
}


Output:

anonymous inner class example


Now let’s see an example on how to pass anonymous inner class as method argument.

interface HelloWorld
{
   String print();
}
public class AnonymousInnerDemo
{
   // method accepts object of interface HelloWorld
   public void display(HelloWorld m)
   {
      System.out.println(m.print() + "example of anonymous inner class as method argument.");
   }
   public static void main(String[] args)
   {
      AnonymousInnerDemo obj = new AnonymousInnerDemo();
      // pass an anonymous inner class as an argument
      obj.display(new HelloWorld() {
         public String print() {
            return "Hi! ";
         }
      });
   }
}


Output:

Hi! example of anonymous inner class as method argument.