Explain Nested Class And Its Types in Java

Share

What is a Nested Class?

A class can also be declared as a member of another class, just like instance variables and instance methods. So, when a class is defined in another class then it is known as a nested class in Java. A nested class never exists independently without an outer class. Therefore, its scope limits within the outer class only.

Point to remember:-

  • A nested class is considered as a member of the enclosing class.
  • The nested class has access to instance variables and instance methods of the outer class, even private members.
  • Enclosing class has no access over members of the nested class.
Note : Inner Class and Non-Static class are the same thing

 

Types of Nested Class in Java

There are broadly two types of nested classes:

1) Static
2) Inner class

  • non-static class
  • local class
  • Anonymous class

Both categories are distinguished from one another in a way that static class doesn’t require an object of the outer class in order to instantiate itself, whereas inner class requires an instance of the outer class in order to instantiate itself.

Static Nested Class in Java

As explained above, it can be instantiated without the help of instance of the outer class. It is preceded by the static keyword. It is not possible to access non-static members of the outer class directly. Therefore, it is not frequently used. [ Read about static keyword and its uses ]

Syntax:

class A
{
  static class B
  {
  }
}

Java program for static nested class

Program:

public class Test
{
  static int y;   //Static variable 
    Test ()   // constructor
  {
    System.out.println ("Hi!I am constructor");
  } 

static class nestedClass{
    void display ()
    {
      System.out.println ("I am from static nested class");
    }
  }
  public static void main (String[]args)
  {
    Test obj = new Test ();
    Test.nestedClass o = new Test.nestedClass ();
    o.display ();
  }
}

Output:

Hi!I am constructor

I am from static nested class

Inner Class or Non-Static Nested Class in Java

These are similar to the data member and instance method of a class that requires an object or instance of enclosing class in order to instantiate themselves. It can access static and non-static members of the outer class directly.

Syntax:

class A{ 
class B{
}
}

Java program for Inner Class or Non-Static Nested Class

Program:

public class OuterClass
{
  OuterClass ()
  {
    InnerClass obj = new InnerClass ();
      obj.display ();
  }
  void show ()
  {
    System.out.println ("Hi! I am outer class.");
  }

  class InnerClass
  {
    void display ()
    {
      System.out.println ("Hi! I am inner class.");
    }
  }
  public static void main (String[]args)
  {
    OuterClass obj = new OuterClass ();
    obj.show ();
  }
}

Output:

Hi! I am inner class.
Hi! I am outer class.

Local Inner Class in Java

A local class is an inner class that is defined within the block or method similar to local variables. It can’t be accessed outside a method or block. It is instantiated with the method or block in which it is defined.

Syntax:

class A
{
  returnType methodName ()
  {
    class B
    {
        
    }
  }
}

Java program for Local Inner Class

Program:

public class OuterClass
{
  void show ()
  {
    // local inner class 
    class localInnerClass
    {
      void display ()
      {
  System.out.println ("Hi! I am local inner class.");
      }

    }
    localInnerClass obj = new localInnerClass ();
    obj.display ();
  }
  public static void main (String[]args)
  {
    OuterClass obj = new OuterClass ();
    obj.show ();
  }

}

Output:

Hi! I am local inner class.

Anonymous Inner Class in Java

An anonymous inner class is a class without having any name. For example, suppose there is a class A having show() method and I want to change the behavior of show method for a specific type of object.

In order to achieve this, we need to create another class B inheriting class A and override the method show.

Another way is Annonymous class, in this, we don’t need to create another class, instead, we define the class body in between curly braces (just like a simple block) without the name of the class for a specific object and override that method that we want. The main purpose of the anonymous inner class is to create concise code.

Its implementation is provided just after the object creation statement but before the semicolon.  It proves handy when it comes to working with event handling.

Point to remember:-

  • It serves only to single object
  • It reduces the length of the code
  • Its implementation is done just after the object creation by putting a curly brace at the front of that object  creation ( new class_name()) statement and in between  the braces provide the behavior of that object
  • It avoids the creation of subclass just for sake of overriding a method
  • It can be used with concrete class, abstract class, and interface.

Syntax:

className obj = new className{
 //body
 }

Java program for Anonymous Inner Class

Program:

class SomeClass
{
  void display ()
  {
    System.out.println ("Hi! I am real one.");
  }

}
public class OuterClass
{
  public static void main (String[]args)
  {
    SomeClass obj = new SomeClass (){
      void display (){
          System.out.println ("Hi! I am anonymous inner class.");}

          }; obj.display ();}
          }

Output:

Hi! I am anonymous inner class.

This post was last modified on December 5, 2020

Sandeep Verma

Published by
Sandeep Verma
Tags: Anonymous class inner class java local class Local Inner Class in Java Non-Static Nested Class in Java Static Nested Class in Java Types of Nested Class in Java What is a Nested Class in java