In this article we will see about 'Nested Inner Classes in Java', and how to use Nested Class with example.

Any class declared inside another class is called nested inner class, the scope of an inner class is bound to the scope of outer class where the class is declared.

A Nested Inner Classes is useful when the usage of the class is limited to be used inside the outer class. Inner classes make code more readable and encourage encapsulation in Java.
package com.tb.demo;

class OuterClass {

	class InnerClass {

	}
}
In Java inner classes are of two types 1) Static Inner class, 2) Non Static Inner class.

1) Static Inner class

If a class is declared inside the body of another class with static modifier applied to it, the class is called static inner class. Static inner classes can only use static members of its outer class.
package com.tb.demo;

class OuterClass {

	static class InnerClass {

	}
}

2) Non Static Inner class

If a class is declared inside the body of another class without static modifier applied to it, the class is called non static inner class.

These classes can be declared with public, private, default and protected modifiers, whereas the outer classes can be declared with default and public modifiers only.

Non static Inner classes can access all the members of its outer class directly including private members, whereas the outer class can not access its inner class's members directly.

Inner classes can not be instantiated outside its outer class.
package com.tb.demo;

class OuterClass {

	class InnerClass {

	}
}
Non static inner classes are of three types: 1) Member inner class, 2) Local inner class and 3) Anonymous inner class.

2.1) Member inner class

A class that is created inside the body of another class but outside the body of any method is called member inner class.
package com.tb.demo;

class OuterClass {
	class InnerClass {
	}
}
Methods of member inner class can be accessed inside the outer class using the inner's object:
package com.tb.demo;

class OuterClass {

	public void accessInner() {
		// Inner class object
		InnerClass inn = new InnerClass();
		inn.printName();
	}

	class InnerClass {
		public void printName() {
		}
	}
}
Methods of member inner class can be accessed outside the outer class as shown below:
package com.tb.demo;

class OuterClass {

	class InnerClass {
		public void printName() {
		}
	}
}

class OtherClass{
	void main(){
		OuterClass outerClass = new OuterClass();
		
		// Instantiating inner class using inner class object
		OuterClass.InnerClass innerClass= outerClass.new InnerClass();
		innerClass.printName();
	}
}

2.2) Local inner class

A class that is created inside the body of a method is called member inner class.
package com.tb.demo;

class OuterClass {

	public void printValue() {
		class InnerClass {
public void printName() {
			}
		}
	}
}
Local inner class members can not be accessed outside the method. In order to access the members of local inner class inside the method, an instance of the class must be created inside the method.
package com.tb.demo;

class OuterClass {

	public void printValue() {
		class InnerClass {
			public void printName() {
			}
		}
		InnerClass innerClass = new InnerClass();
		innerClass.printName();
	}

}

2.3) Anonymous inner class

A class declared inside the body of another class without having a name is called anonymous inner class.
package com.tb.demo;

interface Anonymous {
	void type();
}

class OuterClass {

	public void printName() {
		Anonymous anonymous = new Anonymous() { // Annonymous class created
			public void type() {

			}
		};
	}

}
The name of the anonymous inner class is decided by the compiler, these classes are useful when giving implementation to an interface method and using the same method.

In this article we have seen 'Nested Inner Classes in Java', and how to use Nested Class with example. In upcoming articles we will see more about Java.
  • By Techburps.com
  • Nov 7, 2017
  • Core Java