Java Inner Classes (Part 5)

 

Java Inner / Nested Classes 

In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place and creates more readable and maintainable code.

  • The scope of a nested class is bounded by the scope of its enclosing class (Outer Class). Nested Class does not exist independently of class Outer Class.
  • A nested class has access to the members, including private members, of the class in which it is nested. However, the reverse is not true i.e., the enclosing class does not have access to the members of the nested class.
  • A nested class is also a member of its enclosing class.
  • As a member of its enclosing class, a nested class can be declared privatepublicprotected, or default .
Syntax:-
 public class MyClass {  
      // code  
      class InnerClass {  
           // code  
      }  
 }  

Nested Class are divided into two categories.


 A. Static Nested Class

In the case of normal or regular inner classes, without an outer class object existing, there cannot be an inner class object. i.e., an object of the inner class is always strongly associated with an outer class object. 

    But in the case of static nested class, Without an outer class object existing, there may    be a static nested class object. i.e., an object of a static nested class is not strongly associated with the outer class object.

▪ They are accessed using the enclosing class name.


▪ To create a static inner class use the following syntax:-
 OuterClass.InnerClass [obj name] = new OuterClass.InnerClass();  

eg:-
 class OuterClass {  
   
      // static member  
      static int num1 = 10;  
      // private static number  
      private static int num2 = 15;  
      // non-static member  
      int num3 = 20;  
   
      // static inner class  
      static class InnerClass {  
           void getInfo() {  
                // able to display static member  
                System.out.println("Displaying num1: " + num1);  
   
                // able to display private static member  
                System.out.println("Displaying num2: " + num2);  
   
                // not able to display non-static member  
                // System.out.println("Displaying num3: "+num3); //generates error  
           }  
      }  
 }  
   
 public class MyClass {  
      public static void main(String[] args) {  
           // creating object of inner class  
           OuterClass.InnerClass innclass = new OuterClass.InnerClass();  
           innclass.getInfo();  
      }  
Output:-
 Displaying num1: 10  
 Displaying num2: 15  

 

B. Non-Static Nested Class

To instantiate an non-static inner class, you must first instantiate the outer class. Then, create the inner object within the outer object.

There are two types of non-static classes named:
    

1. Local Classes 

Here I have instantiated inner class by enclosing it in a method and calling it after creating an object of outer class. Refer the program below to understand it better.

There is also a weird syntax for local classes but I would recommend you to enclose it in a method as it will give you a better idea of working of inner classes.

eg:-
 class OuterClass {  
   
      // static member  
      static int num1 = 10;  
      // private static number  
      private static int num2 = 15;  
      // non-static member  
      int num3 = 20;  
   
      // Local inner class  
      class InnerClass {  
           void getInfo() {  
                // able to display static member  
                System.out.println("Displaying num1: " + num1);  
   
                // able to display private static member  
                System.out.println("Displaying num2: " + num2);  
   
                // able to display non-static member  
                System.out.println("Displaying num3: " + num3);  
           }  
      }  
   
      public void display() {  
           // creating object of inner class  
           InnerClass obj = new InnerClass();  
           obj.getInfo();  
      }  
 }  
   
 public class MyClass {  
      public static void main(String[] args) {  
           // creating object of outer class  
           OuterClass object = new OuterClass();  
           object.display();  
      }  
 }  
Output:-
 Displaying num1: 10  
 Displaying num2: 15  
 Displaying num3: 20  


2. Anonymous Classes 

Anonymous Classes will be explained in the following tutorials as it is itself a whole concept. So Stay Tuned !
 


< Previous Next >