It is possible to define a class within another class, such classes are know as nested classes.
Example of a Nested class:
class Outer
{
int x = 5;
class Inner
{
x = x+ 1;
System.out.println("The value of x after increment is "+ x);
int y = 10;
System.out.println("The value of y is "+ y);
}
}
In this example, Since the 'Inner' class is inside the 'Outer' class, the 'Inner' class can be mentioned as nested class.
Before implementing the Nested class on Eclipse IDE, its better to know the scope of the Inner class/Nested class.
Scope of the Nested class:
A nested class has access to the members, including private member, of the class in which it is nested. However, the enclosing class does not have access to the members of the nested class.
i.e. In the above example, the instance variable 'x' which is declared in Outer class is accessed, incremented and printed by the Inner class. But the instance variable 'y' which is declared inside the Inner class cannot be accessed by the Outer class.
So, how to access members of the Inner class ?
If the Outer class wants to access the members of the Inner class, we should create an object in Outer class as an instance to Inner class.
Lets implement nested class on Eclipse IDE :
( i..e. Inner class accessing members of the Outer class without creating any object, where as Outer class accessing members of the Inner class by creating an object)
1. Create 'Outer' class under any project as shown below and save:
2. Create 'NestedClassDemo' under the same project as shown below:
3. Save and Run the 'NestedClassDemo' class
4. Observe that the output is displayed in the console as shown below:
Download this Project:
Click here to download the project containing 'Outer' and 'NestedClassDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
Lets implement this negative scenario on Eclipse IDE:
(Going forward lets find out the negative scenario i.e. What happens when the Outer class access the members of the Inner class directly without creating any object.)
1. In the above project, open 'Outer' class file and make the changes as shown below and observe that 'inner_y' and 'display( )' statements have errors:
After looking at errors, its very clear that we can't access the members of the Inner class without creating an object.
Example of a Nested class:
class Outer
{
int x = 5;
class Inner
{
x = x+ 1;
System.out.println("The value of x after increment is "+ x);
int y = 10;
System.out.println("The value of y is "+ y);
}
}
In this example, Since the 'Inner' class is inside the 'Outer' class, the 'Inner' class can be mentioned as nested class.
Before implementing the Nested class on Eclipse IDE, its better to know the scope of the Inner class/Nested class.
Scope of the Nested class:
A nested class has access to the members, including private member, of the class in which it is nested. However, the enclosing class does not have access to the members of the nested class.
i.e. In the above example, the instance variable 'x' which is declared in Outer class is accessed, incremented and printed by the Inner class. But the instance variable 'y' which is declared inside the Inner class cannot be accessed by the Outer class.
So, how to access members of the Inner class ?
If the Outer class wants to access the members of the Inner class, we should create an object in Outer class as an instance to Inner class.
Lets implement nested class on Eclipse IDE :
( i..e. Inner class accessing members of the Outer class without creating any object, where as Outer class accessing members of the Inner class by creating an object)
1. Create 'Outer' class under any project as shown below and save:
4. Observe that the output is displayed in the console as shown below:
Click here to download the project containing 'Outer' and 'NestedClassDemo' class files used in this post (You can download this project and import into Eclipse IDE on your machine)
Lets implement this negative scenario on Eclipse IDE:
(Going forward lets find out the negative scenario i.e. What happens when the Outer class access the members of the Inner class directly without creating any object.)
1. In the above project, open 'Outer' class file and make the changes as shown below and observe that 'inner_y' and 'display( )' statements have errors: