It is also possible to nest classes in Java (a class within a class). Nested classes are being used to combine classes that belong together, making your code easier to read and maintain.
- Making code that is readable and easy to understand.
- The outer class's private methods can be accessed, giving it a new dimension and bringing it closer to reality.
- The code module is being optimized.
Inner classes are divided into two types :
Non-static nested classes:
- Inner Classes
- Method Local Inner Classes
- Anonymous Inner Classes
Static nested classes :
Hierarchy of Inner classes :
class OuterClass {
int num;
// inner class
private class InnerClass {
public void print() {
System.out.println("This is an inner class");
}
}
// Accessing he inner class from the method within
void display_Inner() {
InnerClass obj = new InnerClass();
obj.print();
}
}
public class MyMainClass {
public static void main(String args[]) {
// Instantiating the outer class
OuterClass obj1 = new OuterClass();
// Accessing the display_Inner() method.
obj1.display_Inner();
}
}
Output :
This is an inner class
Accessing Private Members :
Inner classes are also used to gain access to a class's private members. Suppose a class has private members who can only be accessed by the class's members. Create an inner class in it, using a method within the inner class to return the private members.
class OuterClass {
// private variable of the outer class
private int num = 156;
// inner class
public class InnerClass {
public int getMember() {
System.out.println("This is the getMember method of the inner class");
return num;
}
}
}
public class MyMainClass {
public static void main(String args[]) {
// Instantiating the outer class
OuterClass outer = new OuterClass();
// Instantiating the inner class
OuterClass.InnerClass inner = outer.new InnerClass();
System.out.println(inner.getMember());
}
}
Output :
This is the getMember method of the inner class: 156
Method Local Inner Class :
An inner class can be declared within an outer class's method. Like local variables, the scope of the inner class is restricted within the method. Only the method where the inner class is declared can be used to instantiate a method-local inner class.
Example:
public class Outerclass {
// instance method of the outer class
void OuterClsMethod() {
int num = 125;
// method-local inner class
class MethodInnerClass {
public void show() {
System.out.println("This is method inner class " + num);
}
} // end of inner class
// Accessing the inner class
MethodInnerClass inner = new MethodInnerClass();
inner.show();
}
public static void main(String args[]) {
Outerclass outer = new Outerclass();
outer.OuterClsMethod();
}
}
Output :
This is method inner class 125
Anonymous Inner Class :
An anonymous inner class is one which is defined without a class name. We declare and instantiate anonymous inner classes at the same time in this example. They're often used when you need to override a class's or interface's method.
Example :
abstract class Test
{
abstract void eat();
}
class TestAnonymousInner
{
public static void main(String args[])
{
Test t = new Test()
{
void show()
{
System.out.println("hello");
}
};
t.show();
}
}
Output :
hello
Static Nested Class :
A nested class which is a static member of the outer class is called a static inner class. Other static members can be used to access it without having to instantiate the outer class. A static nested class, like static members, has no access to the outer class's instance variables and methods.
Example :
public class Outer
{
static class NestedClass
{
public void TestMethod()
{
System.out.println("This is nested class.");
}
}
public static void main(String args[])
{
Outer.NestedClass nested = new Outer.NestedClass();
nested.TestMethod();
}
}
Output :
This is nested class.