How to Implement Nested Class in Java?

Published on Sep 20,2019 3.8K Views

How to Implement Nested Class in Java?

edureka.co

In Java, a class can be defined within another class and such classes are known as nested classes. These classes help you to logically group classes that are only used in one place. This increases the use of encapsulation and creates a more readable and maintainable code. This blog on “Nested Class in Java” will give you a quick to-the-point introduction to nested classes in the Java language. Below are the topics covered in this blog:

Nested Class in Java

The class written within a class is called the nested class while the class that holds the inner class is called the outer class. Below are some points to remember for nested classes in Java –

Types of nested classes

Inner/Non-static nested class: In Java, non-static classes are a security mechanism. A class cannot be associated with the access modifier private, but if you have the class as a member of other class, then the non-static class can be made private.

Types of inner classes −

Inner Class

To create an inner class you just need to write a class within a class. An inner class can be private which cannot be accessed from an object outside the class. Below is a program to create an inner class. In this example, the inner class is made private and is accessed class through a method.

class Outer_Test {
   int num;
   
   // inner class
   private class Inner_Test {
      public void print() {
         System.out.println("This is an Our inner class");
      }
   }
   
   // Accessing he inner class from the method 
   void display_Inner() {
      Inner_Test inner = new Inner_Test();
      inner.print();
   }
}
   
public class My_class {

   public static void main(String args[]) {
      // Instantiating the outer class 
      Outer_Test outer = new Outer_Test();
      
      // Accessing the display_Inner() method.
      outer.display_Inner();
   }
}

Output

 

Method-local Inner Class

In Java, a class can be written within a method and it is a local type. Similar to local variables, the scope of an inner class is restricted within the method. A method-local inner class is incorporated only within the method where the inner class is defined. The below program shows how to use a method-local inner class.

public class Outerclass {
   // instance method of the outer class 
   void my_Method() {
      int num = 1001;

      // method-local inner class
      class StarInner_Test {
         public void print() {
            System.out.println("This is star inner class "+num);	   
         }   
      } // end of inner class
	   
      // Accessing the inner class
      StarInner_Test star = new StarInner_Test();
      star.print();
   }
   
   public static void main(String args[]) {
      Outerclass outer = new Outerclass();
      outer.my_Method();	   	   
   }
}

Output

Anonymous Inner Class

Anonymous inner class is an inner class declared without a class name. In an anonymous inner class, we declare and instantiate it at the same time. They are generally used when you need to override the method of a class or an interface. The below program shows how to use an anonymous inner class –

abstract class AnonymousInnerTest {
   public abstract void mytest();
}

public class Outer_class {

   public static void main(String args[]) {
      AnonymousInnerTest inner = new AnonymousInnerTest() {
         public void mytest() {
            System.out.println("This is an example of anonymous inner test class");
         }
      };
      inner.mytest();	
   }
}

Output-

Static nested class: A static class is a nested class that is a static member of the outer class. Unlike inner class, the static nested class cannot access member variables of the outer class because the static nested class doesn’t require an instance of the outer class. Hence, there is no reference to the outer class with OuterClass.this. The syntax of a static nested class is –

class MyOuter {
   static class Nested_Test {
   }
}

Example of Static Nested Class

public class Outer {
   static class Nested_Test {
      public void my_method() {
         System.out.println("This is Edureka's nested test class");
      }
   }
   
   public static void main(String args[]) {
      Outer.Nested_Test nested = new Outer.Nested_Test();	 
      nested.my_method();
   }
}

Output

Difference Between Static and Non-static Nested Classes

Static nested classes do not have access to other members of the enclosing class directly. Being static, it must access the non-static members of its enclosing class through an object which means it cannot refer to the non-static members of its enclosing class directly. And due to this restriction, static nested classes are seldom used.

Non-static nested classes have access to all members of its outer class and can refer to them directly in the same way that other non-static members of the outer class do.

Before we come to the end of this article, let us look at a few key points.

Key Points to Remember

With this, we come to an end of this blog on Nested classes in Java. If you wish to learn more about Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Nested Class in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework