Java/J2EE and SOA (305 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (304 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Implement Nested Class in Java?

Published on Sep 20,2019 39 Views
12 / 13 Blog from Java OOPS

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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 –

  • The scope of a nested class is bounded by its enclosing class.
  • A nested class has access to the members of the class in which it is nested. But, the enclosing class cannot access the members of the nested class.
  • A nested class is its enclosing class member.
  • A nested class can be declared public, private, protected, or package-private.

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
  • Method-local Inner Class
  • Anonymous Inner Class

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
Nested class inner- Edureka

 

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

Method-local Inner Class Java - Edureka

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-

Anonymous Class Java - Edureka

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
Nested class static - Edureka

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

  • The inner class is treated as a regular member of a class.
  • Since the inner class is members of the outer class, you can apply different access modifiers like protected, private to your inner class.
  • Since the Nested class is a member of its enclosing class, you can use . (dot) notation in order to access the nested class and its members.
  • Using a nested class makes your code more readable and provides better encapsulation.
  • The inner class has access to other members of the outer class, even if they are declared private.

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.