Nested Classes in Java

In Object Oriented Programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. It is different from the subclass. It is part of Java from the Java 1.1.

In Java we have four types of nested classes:

Anonymous Inner Class

These classes are created to override a method for an object. In it, new methods can be added, but when called results in the compilation error.

Example

class MyClass {
 public void show() {
 System.out.println("Hello World");
 }
 public void display() {
 System.out.println("Hello India");
 }
}
class Test {
 public static void main(String args[]) {
 MyClass m = new MyClass();
 m.show();
 m.display();
 
 MyClass m1 = new MyClass() {
 public void show() {
 System.out.println("Overridden Show.");
 }
 };

m1.show();
 m1.display();

new MyClass() {
 public void show() {
 System.out.println("show once again");
 }
 }.show();
 }
}

Member Inner Class

Class within a class is called member inner class. These classes can be declared as private or public. Naming is done as.Outer$Inner.class  An object of member class can be created within the outer class or outside outer class.

Example

class Abc {

public class Xyz {
 public void show() {
 System.out.println("Show from Xyz.");
 }
 }

public void check() {
 Xyz x = new Xyz();
 x.show();
 // creating an object of inner class within outer class
 }
}

class Test {
 public static void main(String args[]) {
 Abc a = new Abc();
 a.check();

Abc.Xyz b = a.new Xyz();
 b.show();
 //creating an object of inner class outside outer class

Abc.Xyz c = new Abc().new Xyz();
 c.show();
 //creating an object of inner class outside outer class
 }
}

Static Inner Class

An inner class created explicitly using “static” keyword is called static inner class. It can be declared as private or public. Its naming is done as.Outer$Inner.class An object of a static inner class can be created within the outer class or outside outer class. In this, no object of the outer class is required to create an object of the inner class.

Example

class Abc {

public static class Xyz {
 public void show() {
 System.out.println("Show from Xyz.");
 }
 }

public void check() {
 Xyz x = new Xyz();
 x.show();
 // creating an object of inner class within outer class
 }
}

class Test {
 public static void main(String args[]) {
 Abc a = new Abc();
 a.check();

Abc.Xyz b = new Abc.Xyz();
 b.show();
 //creating an object of static inner class outside outer class

}
}

Singleton class

These are the classes, whose only single object is created. This single object is used wherever required.

Example

class Singleton {

private static final Singleton object = null;

public static Singleton getSingleton() {
 if(object == null) {
 object = new Singleton();
 }
 return object;
 }


 public void show() {
 System.out.println("This is show.");
 }

public void check() {
 System.out.println("This is check.");
 }
}

class Test {
 public static void main(String args[]) {
 Singleton sc1 = Singleton.getSingleton();
 sc1.show();
 sc1.check();

Singleton sc2 = Singleton.getSingleton();
 sc2.show();
 sc2.check();
 }
}

Most frequently asked questions on Nested classes in Java?

An inner class is a type of nested class. Inner classes are non-static nested classes i.e the one that is associated with the instance of outer class.
Yes, we can define an inner class within an interface. Inside the interface, the inner class is implicitly public static.
public interface School {
   void Marks();

   public class Student implements School{
      void Marks(){
         System.out.println("Markus");
      }
   }
}
The main difference between static and non-static nested classes is that you need not instantiate the outer class to access static nested classes. But, to access non-static nested classes, you have to instantiate the outer class.