An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler.
An anonymous class is never abstract (§8.1.1.1).
An anonymous class is never final (§8.1.1.2).
The fact that an anonymous class is not final is relevant in casting, in particular the narrowing reference conversion allowed for the cast operator (§5.5). It is also of interest in subclassing, in that it is impossible to declare a subclass of an anonymous class, despite an anonymous class being non-final, because an anonymous class cannot be named by an extends clause (§8.1.4).
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
The superclass or superinterface of an anonymous class is given by the class instance creation expression (§15.9.1), with type arguments inferred as necessary while choosing a constructor (§15.9.3).
If the class instance creation expression uses <> with an anonymous class, then for all non-private methods declared in the anonymous class body, it is as if the method declaration is annotated with @Override (§9.6.4.4).
When <> is used, the inferred type arguments may not be as anticipated by the programmer. Consequently, the supertype of the anonymous class may not be as anticipated, and methods declared in the anonymous class may not override supertype methods as intended. Treating such methods as if annotated with @Override (if they are not explicitly annotated with @Override) helps avoid silently incorrect programs.
15.9.5.1. Anonymous Constructors
An anonymous class cannot have an explicitly declared constructor. Instead, an anonymous constructor is implicitly declared for an anonymous class. The form of the anonymous constructor for an anonymous class C with direct superclass S is as follows:
-
If S is not an inner class, or if S is a local class that occurs in a static context, then the anonymous constructor has one formal parameter for each actual argument to the class instance creation expression in which C is declared.
The actual arguments to the class instance creation expression are used to determine a constructor
csof S, as specified by §15.9.3. The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter ofcs.The constructor body consists of an explicit constructor invocation (§8.8.7.1) of the form
super(...), where the actual arguments are the formal parameters of the constructor, in the order they were declared. The superclass constructor to be invoked iscs. -
Otherwise, the first formal parameter of the constructor of C represents the value of the immediately enclosing instance of
iwith respect to S (§15.9.2, §15.9.3). The type of this parameter is the class type that immediately encloses the declaration of S.The constructor has an additional formal parameter for each actual argument to the class instance creation expression that declared the anonymous class. The n'th formal parameter
ecorresponds to the n-1'th actual argument.The actual arguments to the class instance creation expression are used to determine a constructor
csof S, as specified by §15.9.3. The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter ofcs.The constructor body consists of an explicit constructor invocation (§8.8.7.1) of the form
o.super(...), whereois the first formal parameter of the constructor, and the actual arguments are the subsequent formal parameters of the constructor, in the order they were declared. The superclass constructor to be invoked iscs
In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, as specified in §15.9.3, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.
Note that it is possible for the signature of the anonymous constructor to refer to an inaccessible type (for example, if such a type occurred in the signature of the superclass constructor cs). This does not, in itself, cause any errors at either compile-time or run-time.