Introduction To Nested Class In Java

Introduction

 
In this article, we discuss nested classes (inner classes) in Java.
 

Nested class

 
One of the nice and highly advantageous features of Java is the ability to define nested classes, in other words, classes within classes. In Java classes are either top-level-classes (a class defined directly within a package) or nested classes (classes defined within top-level-classes).
 
Syntax:
 
class MainClass
{
      //some coding here
   class NestedClass
   {
      //some coding here
   }
}
 

Advantages

 
The main advantages of a nested (inner) class are:
  • It shows a special type of relationship, in other words, it has the ability to access all the data members (data members and methods) of the main class including private.
  • They provide easier code because it logically groups classes in only one place.
  • Reduces coding since we need less code to write.

Types

 
This can be divided into two categories:
  1. static class
  2. non-static class is also called as inner classes.

Static Classes

 
This class can't refer directly to their instance variables or methods defined in its enclosing class since they use them only by an object reference. They are accessed by the use of class name:
 
MainClass.StaticNestedClass
 
For creating an object, the syntax is:
 
MainClass.StaticNestedClass nestedObject =new MainClass.StaticNestedClass();
 
Example
  1. class Main  
  2.   {  
  3.     static int value=430;  
  4.     static class Inner  
  5.       {  
  6.         void mesg()  
  7.           {  
  8.             System.out.println("Data value is " + value);  
  9.           }  
  10.       }  
  11.     public static void main(String[] args)  
  12.       {  
  13.         Main.Inner c=new Main.Inner();  
  14.         c.mesg();  
  15.       }  
  16.   }    
Output
 
fig-1.jpg
 

Non-static class (Inner class)

 
This class refers directly to its instance variables or methods defined in its enclosing class. Also, because an inner class is associated with an instance, it cannot define any static members itself.
 
The syntax for a non-static class is:
 
class MainClass
  {
    //Some code here
    class InnerClass
      {
        //some code here
      }
  }
 
Example
 
In this example, we are invoking the methods of the member Inner1 class from the dsply() method of the Main1 class.
  1. class Main1  
  2.   {  
  3.     private int value=330;  
  4.     class Inner1  
  5.       {  
  6.         void passmsg()  
  7.           {  
  8.             System.out.println("value is " + value);  
  9.           }  
  10.       }  
  11.     void dsply()  
  12.       {  
  13.         Inner1 inr=new Inner1();  
  14.         inr.passmsg();  
  15.       }  
  16.     public static void main(String args[])  
  17.       {  
  18.         Main1 m=new Main1();  
  19.         m.dsply();  
  20.       }  
  21.   }  
Output
 
fig-2.jpg
 
Example
 
In this example, we are invoking the messg() method of the Inner2 class from outside the class, in other words, the Print class.
  1. class Main2  
  2.   {  
  3.     private int value=430;  
  4.     class Inner2  
  5.       {  
  6.         void messg()  
  7.           {  
  8.             System.out.println("Vaule is " +value);  
  9.           }  
  10.       }  
  11.   }  
  12. class Print  
  13.   {  
  14.     public static void main(String args[])  
  15.       {  
  16.         Main2 m2=new Main2();  
  17.         Main2.Inner2 inr = m2.new Inner2();  
  18.         inr.messg();  
  19.       }  
  20.   }  
Output
 
fig-3.jpg


Similar Articles