Nested and inner classes
Monday, December 4, 2023
Nested and Inner Classes in Java are defined within another class and have access to the members of the containing class, including private members.
There are two main types of nested classes in Java: static inner classes and non-static inner classes.
Inner Classes (Non-Static)
Non-static inner classes are those defined within another class without the static
keyword. They have access to all members of the containing class, including private members. Additionally, they have implicit access to the instance of the containing class.
public class OuterClass {
private int externalData = 10;
// Non-static inner class
public class InnerClass {
public void showExternalData() {
System.out.println("External data from inner class: " + externalData);
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
inner.showExternalData();
}
}
In this example, the class OuterClass
has an attribute named externalData
initialized with the value 10. It contains a non-static inner class called InnerClass
. The inner class has a public method named showExternalData()
, which prints the value of externalData
from the inner class.
The non-static inner class InnerClass
has access to all members of the outer class, including the private member externalData
. It contains a method showExternalData()
that prints the value of externalData
from the inner class.
In the main
method, an instance of the outer class (OuterClass
) is created called outer
. Then, an instance of the inner class (InnerClass
) is created using the syntax outer.new InnerClass()
. Finally, the method showExternalData()
is called from the instance of the inner class, printing the value of externalData
from the inner class.
Static Inner Classes
Static inner classes are similar to non-static ones but are defined with the static
keyword. Unlike non-static classes, they do not have access to the members of the containing class without an instance of it.
public class OuterClass {
private static int staticData = 20;
// Static inner class
public static class StaticInnerClass {
public void showStaticData() {
System.out.println("Static data from static inner class: " + staticData);
}
}
public static void main(String[] args) {
OuterClass.StaticInnerClass staticInner = new OuterClass.StaticInnerClass();
staticInner.showStaticData();
}
}
The class OuterClass
has a static member named staticData
initialized with the value 20. It contains a static inner class named StaticInnerClass
.
The static inner class StaticInnerClass
does not have access to non-static members of the outer class. However, it can access static members. It contains a public method named showStaticData()
that prints the value of staticData
from the static inner class.
In the main
method, an instance of the static inner class (StaticInnerClass
) is created using the syntax OuterClass.StaticInnerClass
. Then, the method showStaticData()
is called from the instance of the static inner class, printing the value of staticData
from the static inner class.
Share it
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
Copied
The code has been successfully copied to the clipboard.