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 unqualified:
The ClassOrInterfaceTypeToInstantiate must denote a class that is accessible, non-
final, and not an enum type; or denote an interface that is accessible. Otherwise a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with
<>, then a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with TypeArguments, then ClassOrInterfaceTypeToInstantiate must denote a well-formed parameterized type (§4.5), or a compile-time error occurs.
Let T be the type denoted by ClassOrInterfaceTypeToInstantiate. If T denotes a class, then an anonymous direct subclass of T is declared. If T denotes an interface, then an anonymous direct subclass of
Objectthat implements 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.
-
If the class instance creation expression is qualified:
The ClassOrInterfaceTypeToInstantiate must unambiguously denote an inner class that is accessible, non-
final, not an enum type, and a member of the compile-time type of the Primary expression or the ExpressionName. Otherwise, a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with
<>, then a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with TypeArguments, then ClassOrInterfaceTypeToInstantiate must denote a well-formed parameterized type, or a compile-time error occurs.
Let T be the type denoted by ClassOrInterfaceTypeToInstantiate. An anonymous direct subclass of 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:
-
If the class instance creation expression is unqualified:
The ClassOrInterfaceTypeToInstantiate must denote a class that is accessible, non-
abstract, and not an enum type. Otherwise, a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with
<>, but the class denoted by ClassOrInterfaceTypeToInstantiate is not generic, then a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with TypeArguments, then ClassOrInterfaceTypeToInstantiate must denote a well-formed parameterized class, or a compile-time error occurs.
The class being instantiated is the class denoted by ClassOrInterfaceTypeToInstantiate.
-
If the class instance creation expression is qualified:
The ClassOrInterfaceTypeToInstantiate must unambiguously denote an inner class that is accessible, non-
abstract, not an enum type, and a member of the compile-time type of the Primary expression or the ExpressionName.If ClassOrInterfaceTypeToInstantiate ends with
<>, and the class denoted by ClassOrInterfaceTypeToInstantiate is not generic, then a compile-time error occurs.If ClassOrInterfaceTypeToInstantiate ends with TypeArguments, then ClassOrInterfaceTypeToInstantiate must denote a well-formed parameterized class, or a compile-time error occurs.
The class being instantiated is the class denoted by ClassOrInterfaceTypeToInstantiate.