views:

52

answers:

4

What is the use of an inner class in a Java server side application? Please explain the benefits other than Swing component containment hierarchy using inner class.

+1  A: 

The benefits of anonymous inner classes are exactly the same as for Swing; i.e. they allow you to implement callbacks without the "programming overhead" of separate class file.

Of course, this is all syntactic sugar. The compiled code for (and performance of) the two approaches are pretty much the same. Indeed, the JVM makes no real distinction between regular and inner / nested classes.

All of the inner/outer scoping stuff is resolved at compile time and translated into a hidden constructor parameter and a hidden variable in the inner instance that refers to the outer instance. You can simulate this all yourself in non-nested classes by coding explicit parameters / variables. Hence it is syntactic sugar.

The only thing that you can do with inner classes that you cannot do with separate classes is access private members. To achieve this using separate classes you would need to change the relevant private members to package private. But I'd still classify that as syntactic sugar ... or close enough.

Stephen C
sorry the benefits are not necessarily exactly the same as for Swing, nor is it all syntactic sugar: there are scoping issues that might be pertinent
George Jempty
+1  A: 

Unless they are not static, inner classes are scoped merely to their containing class (as opposed to the entire package) which is appropriate if their benefit is only to the containing class. A very specific instance where this might be the case is when implementing the strategy pattern. This is a way of using different classes to implement the same algorithm, an example being breadth-wise vs. depth-wise directory recursion. If only the class/method that uses this algorithm needs to have knowledge of the implementation of the algorithm abstracted by the strategy class instances, why expose that to the entire package with non-inner classes if not necessary?

George Jempty
Sorry. You could do all of these things in a Swing application.
Stephen C
+1  A: 

Some classes do specific tasks.

I did write some inner classes in Server Side Apps such as SpecificPageValidator. Commonly, some Validator do some check on this or that. But this page gives very very special inputs. Since i was told no other page will give these rare inputs, i wrote them as inner classes and doesn't publish it in any package.

Another example is to write a class like WeirdEventListener which only allowed in a page or two.

After all, writing down some inner classes can be considered 'hard to maintain' since (usually) no one but the Programmer him/herself knows about it.

jancrot
+1  A: 

I follow the general rule of thumb of using inner classes for instances where the functionality is discrete to the containing class and the functionality unlikely to be re-usable outside of the containing class.

Ashley Walton