Determining the Class being Instantiated
15.9.1 Determining the Class being Instantiated
If the class instance creation expression ends in a class body, then the class being instantiated is an anonymous class. Then:
• If the class instance creation expression is an unqualified class instance creation expression, then TypeDeclSpecifier TypeArgumentsOrDiamond opt must denote
a type, T (§4.3, §4.5), or a compile-time error occurs. It is a compile-time error if the class or interface named by T is not accessible
(§6.6) or if T is an enum type (§8.9). If T denotes a class, then an anonymous direct subclass of the class named by T
is declared. It is a compile-time error if the class denoted by T is a final class. If T denotes an interface, then an anonymous direct subclass of Object that
implements the interface named by T is declared. In either case, the body of the subclass is the ClassBody given in the class instance
creation expression. The class being instantiated is the anonymous subclass.
• Otherwise, the class instance creation expression is a qualified class instance creation expression.
It is a compile-time error if the Identifier after the new token is not the simple name (§6.2) of an accessible (§6.6) non- final inner class (§8.1.3) that is a member of the compile-time type of the Primary.
It is a compile-time error if the name is ambiguous (§8.5) or denotes an enum type.
When TypeArguments are provided after the name, it is a compile-time error if the type arguments, when applied to the named class, do not denote a well- formed parameterized type (§4.5).
Let T be the type named by the Identifier and any type arguments. An anonymous direct subclass of the class named by T is declared. The body of the subclass is the ClassBody given in the class instance creation expression.
The class being instantiated is the anonymous subclass. If a class instance creation expression does not declare an anonymous class, then:
EXPRESSIONS Determining Enclosing Instances 15.9.2
• If the class instance creation expression is an unqualified class instance creation expression, then the TypeDeclSpecifier must denote a class that is accessible (§6.6) and is not an enum type and not abstract , or a compile-time error occurs.
When TypeArguments are provided after the class name, it is a compile-time error if the type arguments, when applied to the class, do not denote a well-formed parameterized type (§4.5).
When " <> " is used to elide type arguments after the class name, it is a compile- time error if the class is not generic.
The class being instantiated is the class denoted by the TypeDeclSpecifier. • Otherwise, the class instance creation expression is a qualified class instance
creation expression. It is a compile-time error if the Identifier after the new token is not the simple
name (§6.2) of an accessible (§6.6) non- abstract inner class (§8.1.3) C that is
a member of the compile-time type of the Primary. It is a compile-time error if the name is ambiguous (§8.5) or denotes an enum
type (§8.9). When TypeArguments are provided after the name, it is a compile-time error
if the type arguments, when applied to C , do not denote a well-formed parameterized type (§4.5).
When " <> " is used to elide type arguments after the name, it is a compile-time error if C is not generic.
The class being instantiated is C .
The type of the class instance creation expression is the class type being instantiated, as defined above.