Monday, June 1, 2009

Why do we use Nested Classes in Java?

Nested Classes are classes defined inside the body of another enclosing class. They are of two types - static and non-static. Read more about them in this article - Nested Class & Inner Classes in Java >>

Why do we use Nested Classes?

Well... there may be a variety of reasons, but the main reason is to use them is probably for a better grouping of classes and hence an improved redability of the code. This will in turn make the code better from the maintainability point of view as well as all these are quite inter-related. By better grouping we mean that the code will be right there where it'll be used. Nesting of classes will provide better packaging convenience as well.

If a class is of use for only one other class, it'll never be a good idea to make that class a top-level class. Putting the definition of the class inside the body of the top-level class (which will use it) will make the code available right at the place where it'll be used. If the code is being used by only one class, why to have a separate class then? Well... this is a design issue and it's normally done to logically group similar information inside a class. As creating a nested class will represent a collective data contained by that nested class, which otherwise would be scattered inside the body of the enclosing class.

Nested classes are treated as members of the enclosing classes and hence we can specify any of the four access specifiers - private, package, protected, or public. We don't have this luxury with top-level classes, which can only be declared public or package.

Potential Disadvantages of Nested Classes in Java

There are no serious disadvantages, but one can certainly figure out at least few including:-
  • Difficult to understand - especially for non-experiences programmers, who may find it difficult to code, enhance, and maintain.
  • More number of classes - it certainly increases the total number of classes being used by the application. For every class loaded into the memory, JVM creates an object of type Class for it. There may be some other routine tasks, which JVM might be required to do for all the extra classes. This may result in a slightly slower performance if the application is using several nested/inner classes (may be due to a poor design).
  • Limited support by the Tools/IDE - Nested classes don't enjoy the same support as the top-level classes get in most of the tools and IDEs. This may irritate the developer at times.

Thanks to the original author and the source

No comments:

Post a Comment