If ClassOrInterfaceTypeToInstantiate ends with TypeArguments (rather than <>), then ClassOrInterfaceTypeToInstantiate must denote a well-formed parameterized type (§4.5), or a compile-time error occurs.
If ClassOrInterfaceTypeToInstantiate ends with <>, but the class or interface denoted by the Identifier in ClassOrInterfaceTypeToInstantiate is not generic, then a compile-time error occurs.
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, then:
The Identifier in ClassOrInterfaceTypeToInstantiate must denote either a class that is accessible, freely extensible (§8.1.1.2), and not an enum class, or an interface that is accessible and freely extensible (§9.1.1.4). Otherwise, a compile-time error occurs.
If the Identifier in ClassOrInterfaceTypeToInstantiate denotes a class, C, then an anonymous direct subclass of C is declared. If TypeArguments is present, then C has type arguments given by TypeArguments; if
<>is present, then C will have its type arguments inferred in §15.9.3; otherwise, C has no type arguments. 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 Identifier in ClassOrInterfaceTypeToInstantiate denotes an interface, I, then an anonymous direct subclass of
Objectthat implements I is declared. If TypeArguments is present, then I has type arguments given by TypeArguments; if<>is present, then I will have its type arguments inferred in §15.9.3; otherwise, I has no type arguments. 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, then:
The Identifier in ClassOrInterfaceTypeToInstantiate must unambiguously denote an inner class that is accessible, freely extensible, not an enum class, and a member of the compile-time type of the Primary expression or the ExpressionName. Otherwise, a compile-time error occurs.
Let the Identifier in ClassOrInterfaceTypeToInstantiate denote a class, C. An anonymous direct subclass of C is declared. If TypeArguments is present, then C has type arguments given by TypeArguments; if
<>is present, then C will have its type arguments inferred in §15.9.3; otherwise, C has no type arguments. 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, then:
The Identifier in ClassOrInterfaceTypeToInstantiate must denote a class that is accessible, non-
abstract, and not an enum class. Otherwise, a compile-time error occurs.The class being instantiated is specified by the Identifier in ClassOrInterfaceTypeToInstantiate. If TypeArguments is present, then the class has type arguments given by TypeArguments; if
<>is present, then the class will have its type arguments inferred in §15.9.3; otherwise, the class has no type arguments. -
If the class instance creation expression is qualified, then:
The ClassOrInterfaceTypeToInstantiate must unambiguously denote an inner class that is accessible, non-
abstract, not an enum class, and a member of the compile-time type of the Primary expression or the ExpressionName.The class being instantiated is specified by the Identifier in ClassOrInterfaceTypeToInstantiate. If TypeArguments is present, then the class has type arguments given by TypeArguments; if
<>is present, then the class will have its type arguments inferred in §15.9.3; otherwise, the class has no type arguments.