---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 26 Edition"
java_version: 26
chapter: "Chapter 15. Expressions"
section: "15.9.5"
heading: "15.9.5. Anonymous Class Declarations"
parent: "15.9. Class Instance Creation Expressions"
source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-15.html#jls-15.9.5
id: "15.9.5"
url: https://jvm.obfuscated.software/jls/se26/15.9.5.html
raw: https://jvm.obfuscated.software/jls/se26/15.9.5.md
---

# 15.9.5. Anonymous Class Declarations

An anonymous class is implicitly declared by a class instance creation expression or by an enum constant that ends with a class body ([§8.9.1](8.9.1.md)).

An anonymous class is never `abstract` ([§8.1.1.1](8.1.1.1.md)).

An anonymous class is never `sealed` ([§8.1.1.2](8.1.1.2.md)), and thus has no permitted direct subclasses ([§8.1.6](8.1.6.md)).

An anonymous class declared by a class instance creation expression is never `final` ([§8.1.1.2](8.1.1.2.md)).

An anonymous class declared by an enum constant is always `final`.

An anonymous class being non-`final` is relevant in casting, in particular the narrowing reference conversion allowed for the cast operator ([§5.5](5.5.md)). On the other hand, it is not relevant to subclassing, because it is impossible to declare a subclass of an anonymous class (an anonymous class cannot be named by an `extends` clause) despite the anonymous class being non-`final`.

An anonymous class is always an inner class ([§8.1.3](8.1.3.md)).

Like a local class or interface ([§14.3](14.3.md)), an anonymous class is not a member of any package, class, or interface ([§7.1](7.1.md), [§8.5](8.5.md)).

The direct superclass type or direct superinterface type of an anonymous class declared by a class instance creation expression is given by the expression ([§15.9.1](15.9.1.md)), with type arguments inferred as necessary while choosing a constructor ([§15.9.3](15.9.3.md)). If a direct superinterface type is given, the direct superclass type is `Object`.

The direct superclass type of an anonymous class declared by an enum constant is the type of the declaring enum class.

The *ClassBody* of the class instance creation expression or enum constant declares fields ([§8.3](8.3.md)), methods ([§8.4](8.4.md)), member classes ([§8.5](8.5.md)), member interfaces ([§9.1.1.3](9.1.1.3.md)), instance initializers ([§8.6](8.6.md)), and static initializers ([§8.7](8.7.md)) of the anonymous class. The constructor of an anonymous class is always implicit ([§15.9.5.1](15.9.5.1.md)).

If a class instance creation expression with a *ClassBody* uses a diamond (`<>`) for the type arguments of the class to be instantiated, then for all non-`private` methods declared in the *ClassBody*, it is as if the method declaration is annotated with `@Override` ([§9.6.4.4](9.6.4.4.md)).

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 or enum constant that declares C.

  The actual arguments to the class instance creation expression or enum constant are used to determine a constructor `x` of S, as specified in [§15.9.3](15.9.3.md). The type of each formal parameter of the anonymous constructor is made identical to the corresponding formal parameter of `x`.

  The anonymous constructor body consists of an explicit constructor invocation ([§8.8.7.1](8.8.7.1.md)) of the form `super(...)`, where the actual arguments are the formal parameters of the anonymous constructor, in the order they were declared. The superclass constructor to be invoked is `x`.

- Otherwise, the first formal parameter of the anonymous constructor represents the value of the immediately enclosing instance of `i` with respect to S ([§15.9.2](15.9.2.md)). The type of this parameter is the class type that immediately encloses the declaration of S.

  The anonymous 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 corresponds to the *n-1*'th actual argument.

  The actual arguments to the class instance creation expression are used to determine a constructor `x` of S, as specified in [§15.9.3](15.9.3.md). The type of each formal parameter of the anonymous constructor is made identical to the corresponding formal parameter of `x`.

  The anonymous constructor body consists of an explicit constructor invocation of the form `o.super(...)`, where `o` is the first formal parameter of the anonymous 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 is `x`.

In all cases, the `throws` clause of an anonymous constructor lists all the checked exceptions thrown by the explicit constructor invocation contained in the anonymous constructor, as specified in [§15.9.3](15.9.3.md), 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 `x`). This does not, in itself, cause any errors at either compile-time or run-time.
