Anonymous Classes
Home ] Up ] Static Member Classes ] Member Classes ] Local Classes ] [ Anonymous Classes ]

 

Example
Running the Example

 

An anonymous class is a local class without a name. 

It combines the syntax for class definition with the syntax for object instantiation.

Interfaces cannot be defined anonymously.

This is the ultimate extension of what we've been doing over the past few examples.

Note:

  • The syntax for anonymous classes is somewhat strange, and sometimes hard to format readably.
  • If the name following the new is the name of a class, the anonymous class is a subclass of the named class.
  • If the name following the new is the name of an interface, the anonymous class is an implementation of the named interface, and is a subclass of Object.
  • There is no way you can use an explicit extends or implements clause.
  • Any arguments you specify are implicitly passed to the superclass constructor
  • Anonymous classes allow you to define a "one-shot class" exactly where it is needed.
  • Anonymous classes cannot have static members
  • Anonymous classes may not be declared public, protected, private, or static
 

This page was last modified on 02 October, 2007