Monday, June 16, 2008

Why do we use Nested Classes in Java?

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.

Update [02-June-2009]: Inner classes (non-static nested classes) in Java are mainly used for - implementing callback, providing alternate interface to an existing class, accessing local final variables, and for adding behavior to GUI. Read more - Why Java needs Inner Classes >>

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.



Frank Silbermann said...

We need nested classes primarily because Java does not provide closures.

For example, suppose one component (e.g. a GUI widget) raises events and invites users of the components to provide event handlers for it to call when the event occurs. The event handler is an object containing one or more event handling methods.

The tasks that must be performed for the user must be defined within the user's context, so that the user's internal data can be referenced and modified when the event occurs. Therefore, the event handling object the user gives to the event-raising component is an instance of an inner class (possibly anonymous) constructed inside the user object.

Geek said...

Yeah, I agree. Though, it's tough to say if that's the main reason why Java supports nested classes in general. What you are talking about is one of the main usage of Inner Classes (a type of nested class), especially anonymous inner classes as you have rightly said.

I've couple of articles here discussing Inner Classes and their usages in Java. I should have given a reference to those articles here without which the topic 'Need for nested classes in Java' is actually incomplete. I appreciate your feedback. Thanks. Keep visiting/posting!