Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Readers ponder the virtues (and defects) of anonymous inner classes

Thinking about using anonymous inner classes in your code? Before you make your move, find out what the Java developer community has to say about them

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

When I put up this last poll, it crossed my mind that a straightforward programmatic question (read: no mention of the mega companies' mishaps, misdeeds, or missteps) wouldn't generate much interest among our readers. Silly me. You are developers after all, and the fundamentals really do matter to you. Thanks. You've renewed my faith.

The topic for this survey actually came down to me from Design Techniques columnist Bill Venners. (Thanks, Bill!) He wanted to test the waters to see if anonymous inner classes were a viable topic for his column.

I asked readers to consider the idea of anonymous inner classes in the context of maintaining another developer's code. Here's what they had to say:

  • 55% don't mind anonymous inner classes as long as they're small

  • 19% don't have any problem with anonymous inner classes, whatever their size

  • 19% dislike anonymous inner classes because they make the code too hard to read

  • 5% were unsure, or didn't have a strong opinion



Looks to me like Design Techniques will be featuring anonymous inner classes as its subject sometime soon. Of the over 1,335 voters, 74 percent, or 988 people, feel anonymous inner classes are just short of a miracle. Of course, nothing is ever cut and dried. Bill will have to sift through the spectrum if he wants to learn why some readers are skeptical and others find the thought of anonymous inner classes positively vile. Well, maybe not vile, but some feel that the introduction of anonymous inner classes in JDK 1.1 gives Java's OO framework a big, fat kick in the pants. We've culled some of the more interesting comments from the poll page. To see more, head to our archive of past polls. And, of course, don't forget to voice your opinion in our latest poll.

I don't mind anonymous inner classes as long as they're small (55%):

I find that anonymous inner classes actually enhance readability when they're small. It's better to see the functionality "right there" than to scan the file for the definition of a named inner (or non-inner) class. I find them useful not only for the "classical" adapter user, but also for such things as simple Thread classes.

Nice feature, ugly syntax.

The key word here is anonymous. In my view, these classes are meant to be used only by the class in which they are contained. Super classes should only make use of these classes through well-defined interfaces. They are a very powerful tool for hiding complexity, but can be abused.

Inner classes allow placement of event handlers where they belong -- with the initialization code for the event source (Component).

It's true that they can make code hard to read; it's up to the programmer to use them intelligently.

As a teacher of Java programming, and as a developer, I advocate using anonymous inner classes when: 1) The intent of the code is clearly commented, and 2) the result is only one simple method body. As with all such "syntactic sugar," whether the use of anonymous inner classes creates a maintenance problem is largely up to the way they are used.