Programming Languages (CS302 2006S)

Notes on Anonymous Inner Classes in Java

Comments on:

The questions:

Comments from: Peter Brown (*), Michael Claveria, Davis Hart, Alex Leach, Angeline Namai.

No Comments from: Brad Miller (x), Mark Nettling (x), Dimitar Tasev (x), Dave Ventresca (x).

Anonymous inner classes override methods of a superclass and access the internal properties and methods of its outer class. They are very useful for controlled access to the components of a class, and are also useful when only one instance of a special class is desired.


Inner classes in general allow the programmer to better encapsulate object data or logic by separating that logic into a subclass. Instead of being limited to a structure like:

            Class 1
            Class 2

Inner classes allow structures like:

            Class 1
            Class 2
                        Class A
                        Class B

Anonymous inner classes offer the same advantages and in addition are useful for callbacks. Instead of using switch blocks or nested if/else blocks, a programmer can provide anonymous inner classes to methods which utilize callback classes. Inner classes have access to the parent's fields, which must be declared as final. Furthermore, inner classes can override the parent's methods.

An anonymous inner class is declared and instantiated in one step, so it saves time, and reduces the number of temporary variables the programmer has to keep track of. Anonymous inner classes also work well in event-handling and in co-routining, because they can act as callbacks. I think that, in this sense, they are analogous to the higher-order procedures of functional languages. Another advantage is that they reduce the number of parent classes (i.e. .java files) that make up one program. Debugging is easier when there are fewer .java files to deal with than when one has to cross-reference a large number of .java files. Also, AIC's allow for better design. Rather than having a separate parent class that contains only one method in it, we could define this method as a member of an anonymous inner class.

One disadvantage I can think of is the confusing syntax, which could make maintenance very difficult. The Java tutorial also says that anonymous inner classes have inner scope. The programmer is therefore forced to declare variables to be final in order to make them accessible to the AIC. I would say that this is somewhat a disadvantage since it adds more complexity to the language.

Question: AIC's increase the number of .class files associated with one .java file. Could this cause any problems?

Inner classes are class files whose name is scoped inside the private scoping of an outer class. Essentially the inner classes are defined and hidden inside another class so that even the other classes in the same package can't see them. An anonymous inner class is an inner class that are not named. Anonymous inner classes can override methods of the superclass. They must use the no parameter constructor of the superclass and have a problem of accessing local variables. An anonymous class is generally used inside an expression and it does not have member scope, since it isn't visible to the enclosing class.

Anonymous inner classes are very useful for controlled access to the innards of another class. They also can be used when you only want one instance of a special class. They are easy to use and quick to type in, plus they hide code that is not being used for anything important in a class. Using anonymous classes to make small changes to class functionality instead of creating a plethora of trivial subclasses actually makes class libraries less cluttered and much easier to understand.

To understand Anonymous Inner Classes, I first had to get a definition for Inner classes in general. Inner Classes are defined simply as a class that is entirely defined within another class. Anonymous inner classes (AICs) are defined simply as unnamed inner classes. Sun states that An anonymous class is generally used inside an expression and it does not have member scope, since it isn't visible to the enclosing class. Anonymous classes can make code difficult to read. You should limit their use to those classes that are very small (no more than a method or two) and whose use is well understood, such as event-handling classes.

I take this quote as meaning that AICs are basically syntactic sugar, which experienced programmers can use to make an inner class and an instance of that inner class at the same time. This allows the programmer to not have to pass arguments to a constructor (in fact, states that one MUST use the no parameter constructor), instead having the inner class simply take what it needs from the larger method enclosing it. AICs are generally only used when only one instance of the class is needed.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 10 09:03:10 2006.
The source to the document was last modified on Wed Mar 8 08:38:42 2006.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,