Sometimes we can declare a Class inside another Class such types of Classes are Called inner Classes. In Java 1.1v inner classes concept was Introduced to fix bugs (GUI bugs) as a part of event handling but because of powerful features and benefits of Inner Classes slowly programmers started using irregular coding also.
We should go for inner classes if there is no chance for the existence of one object without the existence of another type of object.
Example-
There is no chance for existence of engine object without the existance of a vehicle object. We have to declare the Engine class inside the Vehicle class, In this case, Engine class is the inner class and the Vehicle is the outer class.
class Vehicle { //Outer Class
class Engine{
//Inner Class
}
}
Note-
- If there is no outer class then there is no chance of inner Class.
- The Relation between outer Class and Inner Class is a
has a relationship
.
Types of Inner Classes
According to position of declration and behaviour inner Class in Java are divided into four catogries.
- Normal or Regular inner classes
- Method Local inner classes
- Anonymous inner classes
- Static nested classes
Normal or Regular Inner Classes
If we declare any named class inside a class without static modifier, such type of error classes is known Normal or Regular inner classes.
class OuterClass {
public static void main(String[] args) {
System.out.println("inner class main method");
}
class InnerClass{
}
}
![Normal or Regular Inner Classes](https://thecodedata.com/wp-content/uploads/2023/07/image-8.png)
Inside the Inner Class, we can not declare any static member, Hence we can not declare the main method and run the inner Class directly.
Accessing Inner Class code from static or Outer Class.
class OuterClass {
class InnerClass{
public void method1(){
System.out.println("This is Inner class method");
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
inner.method1();
// outer
OuterClass.InnerClass i = new OuterClass().new InnerClass();
i.method1();
}}
![Accessing Inner Class code from static or Outer Class.](https://thecodedata.com/wp-content/uploads/2023/07/image-9.png)
Accessing Inner Class Code from Instance Area of Outer Class
class OuterClass {
class InnerClass{
public void method1(){
System.out.println("This is Inner class method");
}
}
public void method(){
InnerClass inner = new InnerClass();
inner.method1();
}
public static void main(String[] args) {
OuterClass object = new OuterClass();
object.method();
}
}
![Accessing Inner Class Code from Instance Area of Outer Class](https://thecodedata.com/wp-content/uploads/2023/07/image-10.png)
Accessing Inner Class Code from Outside of Outer Class
class OuterClass {
class InnerClass{
public void method1(){
System.out.println("This is Inner class method");
}
}
}
class TheCodeData{
public static void main(String[] args) {
OuterClass obj = new OuterClass();
OuterClass.InnerClass i = obj.new InnerClass();
i.method1();
}
}
![Accessing Inner Class Code from Outside of Outer Class](https://thecodedata.com/wp-content/uploads/2023/07/image-11.png)
Accessing static and non static Member of Outer Claass inside Inner Class
We can access both static and nonstatic members of the outer class from the regular inner class directly.
class OuterClass {
int num = 10;
static int num2 = 100;
class InnerClass{
public void method1(){
System.out.println("This is Inner class method");
System.out.println(num);
System.out.println(num2);
}}
public static void main(String[] args) {
new OuterClass(). new InnerClass().method1();
}
}
![Accessing static and non static Member of Outer Claass inside Innner Class](https://thecodedata.com/wp-content/uploads/2023/07/image-12.png)
- Inside the inner class,
this
always refers to the current inner class object but if we want to refer current outer class object then we have to useouterclass.this
package inncer;
class OuterClass {
int num = 10;
class InnerClass{
int num = 20;
public void method1(){
System.out.println(this.num);
System.out.println(OuterClass.this.num);
}
}
public static void main(String[] args) {
new OuterClass(). new InnerClass().method1();
}
}
![outerclass.this](https://thecodedata.com/wp-content/uploads/2023/07/image-13.png)
Modifiers for Outer Class and Inner Class
The only applicable modifier for outer class are –
- public
- default
- final
- abstract
- strictfp
Applicable Modifier for Inner Class
- public
- default
- final
- abstract
- strictfp
- private
- protected
- static
Nesting of Inner Classes
We can declare Inner Class inside another Inner Class that is nesting of Inner Class is possible.
package inncer;
class OuterClass {
class InnerClass1{
class InnerClass2{
public void method(){
System.out.println("InnerClass2 static method");
}
}
}
}
class TheCodeData{
public static void main(String[] args) {
OuterClass obj = new OuterClass();
OuterClass.InnerClass1 innerClass1 = obj.new InnerClass1();
OuterClass.InnerClass1.InnerClass2 innerClass2 = innerClass1.new InnerClass2();
innerClass2.method();
}
}
![Nesting of Inner Classes](https://thecodedata.com/wp-content/uploads/2023/07/image-14.png)
1 thought on “Inner Classes in Java”