How to Create an Object for Inner Classes in Java

Create an Object for Inner Classes in Java

In this example, We will show you about, How to Create an Object for Inner Classes in Java

Outer.java

package com.dineshkrish;

public class Outer {

	// Method Belongs to Outer Class
	public void display() {
		
		System.out.println("I am Outer Class");
	}
	
	public class Inner {
		
		// Method Belongs to Inner Class
		public void display() {
			
			System.out.println("I am Inner Class");
		}
		
		public class Inner1 {
			
			// Method Belongs to Inner1 Class
			public void display() {
				
				System.out.println("I am Inner1 Class");
			}
			
			
			public class Inner2 {
				
				// Method Belongs to Inner2 Class
				public void display() {
					
					System.out.println("I am Inner2 Class");
				}
				
				public class Inner3 {
					
					// Method Belongs to Inner3 Class
					public void display() {
						
						System.out.println("I am Inner3 Class");
					}
				}
				
			}
			
		}
	}
	
}

InnerClassTest.java

package com.dineshkrish;

public class InnerClassTest {

	public static void main(String[] args) {

		// Creating an Object For Outer Class
		Outer outer = new Outer();

		outer.display();

		// Creating an Object For Inner Class
		Outer.Inner inner = outer.new Inner();

		inner.display();

		// Creating an Object For Inner1 Class
		Outer.Inner.Inner1 inner1 = inner.new Inner1();

		inner1.display();

		// Creating an Object For Inner2 Class
		Outer.Inner.Inner1.Inner2 inner2 = inner1.new Inner2();

		inner2.display();

		// Creating an Object For Inner3 Class
		Outer.Inner.Inner1.Inner2.Inner3 inner3 = inner2.new Inner3();

		inner3.display();

	}
}

Output

I am Outer Class
I am Inner Class
I am Inner1 Class
I am Inner2 Class
I am Inner3 Class