Nested Class in Java

  • Last updated Apr 25, 2024

A class that is declared within another class is called a nested class. The nested class is closely associated with the outer class and has access to its members, including private members.

A nested class is a member of its outer class. A nested class can be declared as private, public, protected or with default(without keyword) scope.

Here's an example of how a Nested class looks like in Java:

public class Outer {

    class Nested {

    }

}

There are of two types of nested classes in Java:

  1. Static Nested Class
  2. Non-static Nested Class


Static Nested Class

A static nested class is declared with the static keyword. A static nested class cannot access other members of its outer class.

Example:

Here's an example of calling a method of a static nested class from another class:

public class OuterClass {

   private static int outerVariable = 7;

   public static class StaticNestedClass {
      public void nestedMethod() {
	 System.out.println("Static nested method: " + outerVariable);
      }
   }

}

In this example, we have a class called OuterClass. Inside the OuterClass, we have a private static variable called outerVariable which is assigned the value 7 and also the static nested class called StaticNestedClass. The StaticNestedClass is declared as a public class and has a method named nestedMethod(). This method is responsible for printing a message that includes the value of the outerVariable.

A static nested class cannot directly access the variables and methods belonging to the outer class. However, it can still access the static variables and methods of the outer class without needing an instance of the outer class.

To use the StaticNestedClass, you can access it using the outer class name (OuterClass.StaticNestedClass). You can create an instance of the StaticNestedClass and call its methods using the instance:

public class AnotherClass {

   public static void main(String[] args) {
      // Create an instance of the static nested class
      OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();

      // Call the method of the static nested class
      nestedObj.nestedMethod();
   }

}

Output:

Static nested method: 7


Non-static Nested Class

A nested class which is declared without the static keyword is called non-static nested class. It is also known as an inner class. It has access to all the members, including private members, of the outer class. It can access these members directly without the need for an object reference. An inner class can be instantiated only within an instance of the outer class.

Example:

Here's an example of calling a method of a non-static nested class from another class:

public class OuterClass {

   private int outerVariable = 7;
    
   public class NestedClass {
      public void nestedMethod() {
         System.out.println("Nested method: " + outerVariable);
      }
   }

}

In this example, we have a class named OuterClass. Inside the OuterClass, we have a private variable called outerVariable which is assigned the value 7 and also the nested class called NestedClass, which is defined as a public class. The NestedClass is associated with the OuterClass, meaning it has access to the members (variables and methods) of the OuterClass, including the private outerVariable.

The NestedClass has a single method called nestedMethod(). This method is public and does not return any value (void). When called, it prints the message "Nested method:" along with the value of the outerVariable.

To call the method of the non-static nested class, from outside the OuterClass, you should typically create an instance of the OuterClass and then use that instance to create an instance of the NestedClass. Through this instance of the NestedClass, you can access and invoke its methods, including the nestedMethod(). For example:

public class AnotherClass {

   public static void main(String[] args) {
	
      // Create an instance of the outer class
      OuterClass outerObj = new OuterClass();

      // Create an instance of the nested class using the outer class instance
      OuterClass.NestedClass nestedObj = outerObj.new NestedClass();

      // Call the method of the nested class
      nestedObj.nestedMethod();
   }

}

Output:

Nested method: 7


The non-static nested or inner class are of two types:

  1. Local type: A nested class when used inside the body of a method is called local nested class.
  2. Anonymous type: class with no name is called anonymous nested class. We cannot create an instance of an anonymous class. It either extends a class or implement an interface.


When to use a Nested Class?

A nested class can be used in the following cases:

  • When a class is useful only to one class then it is logical to keep the both classes together by declaring one class within another class.
  • The use of nested class increases the use of encapsulation by restricting direct access of inner class members from the outside world and thus making the members of nested class more secure. However, the enclosing class can access the members of inner classes.