Benefits of Inner Classes
Home ] Up ] Nested Classes in C++ ] A Stack Class ] Inner Class Types ] [ Benefits of Inner Classes ]

 

 

Whew!  That's a lot of stuff to work through!

So, is it worth it?  Will you ever use nested or inner classes?

Right now, you may not think so, but in fact, as we progress, you'll see some of the benefits of using them.

The main benefits of using inner classes -- especially anonymous classes -- is that you can place your code close to where it's actually used.  In other words, you can increase the level of cohesion of your design.  

If you use an appropriate set of inner classes, you'll find that:

  • The number of source files will decrease -- note that we went from using two .java files in our initial Stack/StackEnumerator implementation, to one .java file in our inner class implementations.
  • The amount of code that you write will tend to be less.  This is often most obvious when you use anonymous classes.
  • Your code will tend to be more focused (more cohesive).

It takes some experience to understand this, to appreciate it, and then to actually do it.

And it isn't just theoretical; we'll be using anonymous classes quite a bit when we get to event handling.

 

This page was last modified on 02 October, 2007