Google+

93. 'Nested' classes







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.

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)




Please comment below to feedback or ask questions.

'String' class will be explained in the next post.



5 comments:

BONZERMAHESH said...

The Sample code download link is not working on any of the page...

Please provide be codebase example projects..

Arun Motoori said...

@BONZERMAHESH - I am able to access. Please double check.

Ranjith Kumar said...

Hi Arun,

I tried creating a static method inside the inner class but could not. It throws an error asking me to either remove the word 'static' from the method or make the Inner class static. Could you please shed some light on this. Why is it not possible to create a static method inside the inner class?

Appreciate the great work you are doing. Keep it up.

Thanks,
Ranjith

Arun Motoori said...

@Ranjith Kumar - Yes, Java wont allow you to create static variable or method inside non-static inner class. To know why, go through the below explanation:

First of all, non-static stuff of a Class reside inside the object where as static stuff wont reside in the object.

If you are creating an object for any Class, the object can be used to access only the non-static stuff as it holds only the non-static stuff.

Hence a Class has static and non-static stuff, non-static stuff is accessed by using objects and static stuff is accessed by using the Class Name.

In this case of nested classes, the inner class is also a member of outer class. A member of a class can be either static or non-static in nature. If you allow the non-static inner class to have static variables or methods, the member i.e. inner class cannot be static or non-static as inner class is of non-static type where as its members are non-static which disturbs the concept of an object in java which holds only the non-static stuff inside. Here in this type of inner class, we cannot either use an object of inner class to access the static stuff of non-static inner class as objects wont hold static stuff. And also we cannot use Outer Class to access the Inner Class members as the Inner Class is non-static in nature. Hence at high level, Outer Class holds either static or non-static members i.e. variables, methods and inner Classes. Hence to have static members inside any inner class, we have to declare the inner class as static type.

Unknown said...

Hi Arun,

I am trying to create object of inner class in Main (NestedClassDemo). Not able to create object of inner class in main. How to resolved this issue.