Skip to main content
Anonymous Inner Classes
- Anonymous inner classes have no name, and their type must be either a
subclass of the named type or an implementer of the named interface.
- An anonymous inner class is always created as part of a statement;
don't forget to close the statement after the class definition with a
curly brace. This is a rare case in Java, a curly brace followed by a
semicolon.
- Because of polymorphism, the only methods you can call on an
anonymous inner class reference are those defined in the reference
variable class (or interface), even though the anonymous class is really
a subclass or implementer of the reference variable type.
- An anonymous inner class can extend one subclass or implement one
interface. Unlike non-anonymous classes (inner or otherwise), an
anonymous inner class cannot do both. In other words, it cannot both
extend a class and implement an interface, nor can it implement more
than one interface.
- An argument-defined inner class is declared, defined, and
automatically instantiated as part of a method invocation. The key to
remember is that the class is being defined within a method argument, so
the syntax will end the class definition with a curly brace, followed
by a closing parenthesis to end the method call, followed by a semicolon
to end the statement: });