logo

Java Inner Classes


Show

Nested Classes

In Java, immediately like process, changeable of a class also can encompass an added class as its affiliate. Writing a class in another is permitted in Java. The class written in is identified as the nested class, and the class that grasps the inner class is named the outer class.

Syntax

Following is the syntax to write a nested class. Here, the class Outer_Demo is the outer class and the class Inner_Demo is the nested class.

class Outer_Demo {
   class Inner_Demo {
   }
}

Nested classes are divided into two types −

  • Non-static nested classes − These are the non-static members of a class.
  • Static nested classes − These are the static members of a class.

Inner Classes

Inner Classes (Non-static Nested Classes)

Inner classes are a refuge instrument in Java. We recognize a class never is connected with the right of entry modifier confidential, however, if we contain the class as a member of additional class, then the internal class can be complete confidential. In addition, this is used to admission the private members of a class.

Inner classes are of 03 forms depending on how and where you classify them. They are −

  • Inner Class
  • Method-local Inner Class
  • Anonymous Inner Class

Inner Class

Developing an inner class is fairlyeasy. You just require writing a class inside a class. Nothing like a class, an internal class could be confidential and once you state an inner class confidential, it never be admission from an object exterior the class.

Subsequent is the program to generate an internal class and right to use it. In the given instance, we build the inner class classified and way in the class during a technique.

Example

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

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

Here you can scrutinize that Outer_Demo is the external class, Inner_Demo is the central class, display_Inner() is the process inside which we are immediate the inner class, and this technique is invoked from the major technique.

If you accumulate and perform the over program, you will obtain the subsequent result

Output

This is an inner class.

Accessing the Private Members

As stated former, inside classes are too used to admission the personal members of a group. Believe, a class is possessing personal members to admittance them. Write an inside class in it, revisit the classified members from a technique within the internal class, say, getValue(), and lastly from another class call the getValue() technique of the inner class.

To instantiate the inside class, originally you encompass to instantiate the external class. Subsequently, utilizing the object of the external class, subsequent is the approach in which you can instantiate the inner class.

Outer_Demo outer = new Outer_Demo();
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();

The following program shows how to access the private members of a class using inner class.

Example

class Outer_Demo {
   // private variable of the outer class
   private int num = 175;  
   
   // inner class
   public class Inner_Demo {
      public int getNum() {
         System.out.println("This is the getnum method of the inner class");
         return num;
      }
   }
}

public class My_class2 {

   public static void main(String args[]) {
      // Instantiating the outer class
      Outer_Demo outer = new Outer_Demo();
      
      // Instantiating the inner class
      Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
      System.out.println(inner.getNum());
   }
}

If you compile and execute the above program, you will get the following result

Output

This is the getnum method of the inner class: 175

Method-local Inner Class

In Java, we could write a class inside a process and this will be a confined type. Like local changeable, the capacity of the internal class is confined within the technique.

A method-local internal set can be instantiated only in the technique where the internal class is defined. The subsequent program illustrates how to employ a method-local internal class.

Example

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

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

If you compile and execute the above program, you will get the following result:

Output

This is method inner class 23

Anonymous Inner Class

An inside class announced with no a class name is identified as an unnamed inner group. In case of unidentified inner classes, we announce and instantiate them at the equivalent time. Usually, they are used every time you require overriding the technique of a class or a line. The syntax of an unidentified inner class is as pursues −

Syntax

AnonymousInner an_inner = new AnonymousInner() {
   public void my_method() {
      ........
      ........
   }   
};

The following program shows how to override the method of a class using an anonymous inner class.

Example

abstract class AnonymousInner {
   public abstract void mymethod();
}

public class Outer_class {

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

If you compile and execute the above program, you will get the following result:

Output

This is an example of anonymous inner class

In the equivalent method, you can supersede the techniques of the existing class as well as the line using an nameless inner class.

Anonymous Inner Class as Argument

Normally, if a way admits an item of an line, an intangible class, or a existing class, then we can execute the line, expand the conceptual class, and bypass the entity to the way. If it is a class, then we can straight pass it to the way.

However, entire three cases, you could exceed an unidentified inner class to the way. Here is the syntax of bypassing an unnamed inner class as a technique argument −

obj.my_Method(new My_Class() {
   public void Do() {
      .....
      .....
   }
});

The following program shows how to pass an anonymous inner class as a method argument.

Example

// interface
interface Message {
   String greet();
}

public class My_class {
   // method which accepts the object of interface Message
   public void displayMessage(Message m) {
      System.out.println(m.greet() +
         ", This is an example of anonymous inner class as an argument");  
   }

   public static void main(String args[]) {
      // Instantiating the class
      My_class obj = new My_class();

      // Passing an anonymous inner class as an argument
      obj.displayMessage(new Message() {
         public String greet() {
            return "Hello";
         }
      });
   }
}

If you compile and execute the above program, it gives you the following result:

Output

Hello, This is an example of anonymous inner class as an argument

Static Nested Class

An inert inner class is a shelled class that is a stationary affiliate of the external class. It can be admission with no instantiating the external class, using other inert members. Just akin to static members, a still nested class does not have contact with the case variables and process of the outer class. The syntax of the static case class is as follows −

Syntax

class MyOuter {
   static class Nested_Demo {
   }
}

Instantiating a still nested group is a bit diverse from instantiating an inside class. The subsequent program explains how to utilize a static shelled class.

Example

public class Outer {
   static class Nested_Demo {
      public void my_method() {
         System.out.println("This is my nested class");
      }
   }
   
   public static void main(String args[]) {
      Outer.Nested_Demo nested = new Outer.Nested_Demo();	 
      nested.my_method();
   }
}

If you compile and execute the above program, you will get the following result

Output

This is my nested class

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.