Saturday, January 7, 2012

Uses of Nested Class in Java

Java Supports Nesting the classes. This means you can place a Java class inside another Java Class. As far as I know this is not supported in some other programming languages . There are many advantages on using nested classes. Nested classes are not used commonly but I prefer it sometimes. Before explaining you some uses of nested classes, I want to explain what nested classes means.

Nested classes means to place a Java class within another class. Nested classes can be used in this way.

class mainone
{
........
      class insideclass
                      {
                             ........................
                        } // ending of insideclass
} //ending of mainone


This way nested class can be used.

Once main importance for using a nested class is that if you use them, you can use the variables in both the classed. This means that if you define a variable at mainone class(as stated up) then you can access them from insideclass class as well.

Usually in Java, nested classes are used in order to organize the task meaning that grouping similar methods and tasks together which makes our program organized.

For an example. You can use nested class while using swing objects. While you use two classes(one class for adding objects) and second class for adding Listeners. This makes you easy and makes you program organized.

Conclusively I would like to add that it is quite difficult to understand nested class without having some practical knowledges. Once you start using nested class and understand the difference between normal situation and the situation using nested classes, you will understand it gradually.

0 comments:

Post a Comment

You can Comment here. Please make some reasonable Comments. This Blog is Dofollow so get a backlink as a Gift.
Be Sure you use Name@Keywords
Using Keywords directly may make your comments difficult to get approved.

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Enter your email address: