Inner Classes in Java
Introduction
In Java, an inner class is a class that is defined within another class. It provides a way to logically group classes that are only used in one place, which can improve code organization and encapsulation. Inner classes have access to the members of the outer class, including private members, and can be instantiated only within the scope of the outer class.
In this article, we will explore an example of an inner class in Java and understand how it works.
Key Concepts
Before diving into the code example, let's clarify a few key concepts related to inner classes:
- Outer Class: The class that contains the inner class is called the outer class. It provides a context for the inner class and can access its members.
- Inner Class: The class defined within the outer class is called the inner class. It has access to the members of the outer class and can be instantiated only within the scope of the outer class.
- Access Modifiers: Inner classes can have different access modifiers, such as public, private, protected, or default. The access modifier determines the visibility of the inner class outside the outer class.
Code Structure
Let's analyze the code provided in the Code Section:
class Outer {
String so = "This is Outer Class";
void display() {
System.out.println(so);
}
void test() {
Inner inner = new Inner();
inner.display();
}
// This is an inner class
class Inner {
String si = "This is Inner Class";
void display() {
System.out.println(si);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.display();
outer.test();
}
}
Code Explanation
The code provided demonstrates the usage of an inner class within the context of an outer class. Let's break it down step by step:
- The Outer class is defined, which contains a string variable so and three methods: display(), test(), and an inner class Inner.
- The display() method in the Outer class simply prints the value of the so variable.
- The test() method in the Outer class creates an instance of the Inner class and calls its display() method.
- The Inner class is defined within the Outer class. It has its own string variable si and a display() method that prints the value of si.
- The InnerClassDemo class is defined separately and contains the main() method. In the main() method, an instance of the Outer class is created, and its display() and test() methods are called.
Code Examples
Let's see the output of running the code:
This is Outer Class
This is Inner Class
The output confirms that the code is functioning as expected. The display() method of the Outer class is called first, printing the value of so. Then, the test() method is called, which creates an instance of the Inner class and calls its display() method, printing the value of si.
Conclusion
In this article, we explored the concept of inner classes in Java. Inner classes provide a way to logically group classes and improve code organization and encapsulation. They have access to the members of the outer class and can be instantiated only within the scope of the outer class. By understanding inner classes, you can leverage their power to write more modular and maintainable code in Java.