Skip to content

JAVA Inner Classes

January 14, 2008
tags:

One class one responsibility : Cohesive Class. Got it. Its good of course“.

But what about the situation when there is a totally different set of responsibilities that actually require to be together as one unit and at the same time should be tightly bound to your class. The inner classes serve the purpose of having class that has an intimate relationship with your class while your class is hidden from others. This is because the inner class is actually a part of your class and has access to all the members of the class including even the members that have been marked private as any other other instance method would have. The inner classes are declared within the outer classes with curly braces as follows:

class OuterClass
{

class InnerClass { }

}

The InnerClass in the above code is an inner class of the OuterClass class. If you compile the outer class, you will get two classes instead of one as follows:

OuterClass.class
OuterClass$InnerClass.class

As you can see that the inner class is associated with the inner class name with a $ sign joining the name of the inner class with the outer class. This is so true to the behavior also, as the inner class would never be accessible without the existence of the outer class. As in an instance of the inner class does not make any sense without an associated instance of the outer class. Moreover you cannot say like java OuterClass$InnerClass to run the main() method in the InnerClass because the InnerClass cannot have any static declaration (main() is static of course !!).

Instantiating Inner Classes: As I already said, the inner class instance does not make any sense without the instance of the outer class to which it is associated or tied to, so in order to instantiate any inner class, you need an instance of the outer class as well. You may instantiate the inner class from within the outer class or outside the outer class.

Instantiating InnerClass From Within any non-static method of the OuterClass: This is fairly simple. As you would know that the outer class methods always has a this reference (except the static methods), so in order to instantiate you can simply write

InnerClass in = new InnerClass();

from any non-static method of the OuterClass.

Instantiating InnerClass From Outside the OuterClass or From a static method Within the OuterClass: In order to instantiate the InnerClass from within a static method of the OuterClass or from anywhere outside the OuterClass you would first need to make a reference of the OuterClass as follows:

OuterClass outerClass = new OuterClass();
OuterClass.InnerClass inc =
outerClass.new InnerClass();

That’s it for now on Inner Classes. there is a lot more to tell about it, but next time (but very soon I guess).

Advertisements
One Comment leave one →
  1. Iqbal permalink
    August 26, 2009 2:05 am

    Can you give some example?so can practice on java as well.Thx u.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: