This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
There are a variety of intended advantages that inner classes offer: - Showing "tight" relationships between two classes by defining one within another. - Keeping 'trivial' objects separate from 'important' ones by keeping them nameless. Anonymous inner classes are frequently used to describe an object that is detailed enough to be separated from a "higher-level" class but dependent enough on the surrounding code that it is best left nameless. - Providing a style of code-writing that seems cumbersome or harder to maintain with normal package scoping.
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
Inner classes have many uses in Java. One common use is implementation of a factory pattern. The host (enclosing) class is declared to be abstract and you define one or more private or proteced inner classes that extend the enclosing class. You then have a static method(s) that will return an instance of one of those inner classes. Inner classes are also used in instances where the inner class is very tightly coupled to the host class and there is little chance of being able to reuse the inner class in some other situation. Anonymous inner classes are used in a similar manner but are usually used in cases that call for the implementation of a simple interface and can be constructed easily on the fly. Static inner classes just create a quick and dirty package extension. It is important to remember that inner classes have complete access to all the state of their host classes. Of course that does not apply to static inner classes.
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joined: Jan 30, 2002
ME have you take over JY's job of posting just seconds before me?
I don't code professionally *yet*, but I find anonymous inner classes VERY friendly...when I have a button or something that is very specific, I can just define right there what I want to have happen (say in an anonymous ActionEvent or mouseClick) and then move on. I don't like having to write an entire separate class just to have a frame exit or something simple like that. Again, I am not a professional and there may be dis-advantages to this style (please let me know if this is so!!), but I sure like it for now. I save creating whole new classes for when I have multiple things going on all over the place...then the code for events (or whatever) is centralized.
-nothing important to say, but learnin' plenty-
Originally posted by David Crossett: I don't like having to write an entire separate class just to have a frame exit or something simple like that. Again, I am not a professional and there may be dis-advantages to this style (please let me know if this is so!!), but I sure like it for now.
Anonymous inner classing can be a real drag to read, maintain and debug. That's the big drawback. It also opens the door to a few perverse, if amusing, code styles that I won't go into. Don't want to scare the young 'uns.