Let C be the class being instantiated. To create an instance of C, i, a constructor of C is chosen at compile time by the following rules:
First, the actual arguments to the constructor invocation are determined:
-
If C is an anonymous class with direct superclass S, then:
- If S is not an inner class, or if S is a local class that occurs in a static context, then the arguments to the constructor are the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the expression.
- Otherwise, the first argument to the constructor is the immediately enclosing instance of
iwith respect to S (§15.9.2), and the subsequent arguments to the constructor are the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the class instance creation expression.
-
If C is a local class or a
privateinner member class, then the arguments to the constructor are the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the class instance creation expression. -
If C is a non-
privateinner member class, then the first argument to the constructor is the immediately enclosing instance ofi(§8.8.1, §15.9.2), and the subsequent arguments to its constructor are the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the class instance creation expression. -
Otherwise, the arguments to the constructor are the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the expression.
Second, a constructor of C and corresponding return type and throws clause are determined:
-
If the class instance creation expression uses
<>to elide class type arguments, a list of methodsm_1...m_nis defined for the purpose of overload resolution and type argument inference.Let
c_1...c_nbe the constructors of class C. Let#mbe an automatically generated name that is distinct from all constructor and method names in C. For all j (1 ≤ j ≤ n),m_jis defined in terms ofc_jas follows:-
A substitution θ_j is first defined to instantiate the types in
c_j.Let F_1...F_p be the type parameters of C, and let G_1...G_q be the type parameters (if any) of
c_j. Let X_1...X_p and Y_1...Y_q be type variables with distinct names that are not in scope in the body of C.θ_j is
[F_1:=X_1, ..., F_p:=X_p, G_1:=Y_1, ..., G_q:=Y_q]. -
The modifiers of
m_jare those ofc_j. -
The type parameters of
m_jare X_1...X_p,Y_1...Y_q. The bound of each parameter, if any, is θ_j applied to the corresponding parameter bound in C orc_j. -
The return type of
m_jis θ_j applied to C<F_1,...,F_p>. -
The name of
m_jis#m. -
The (possibly empty) list of argument types of
m_jis θ_j applied to the argument types ofc_j. -
The (possibly empty) list of thrown types of
m_jis θ_j applied to the thrown types ofc_j. -
The body of
m_jis irrelevant.
To choose a constructor, we temporarily consider
m_1...m_nto be members of C. Then one ofm_1...m_nis selected, as determined by the class instance creation's argument expressions, using the process specified in §15.12.2.If there is no unique most specific method that is both applicable and accessible, then a compile-time error occurs.
Otherwise, where
m_jis the selected method,c_jis the chosen constructor. The return type andthrowsclause ofc_jare the same as the return type andthrowsclause determined form_j(§15.12.2.6). -
-
Otherwise, the class instance creation expression does not use
<>to elide class type arguments.Let T be the type denoted by C followed by any class type arguments in the expression. The process specified in §15.12.2, modified to handle constructors, is used to select one of the constructors of T and determine its
throwsclause.If there is no unique most-specific constructor that is both applicable and accessible, then a compile-time error occurs (as in method invocations).
Otherwise, the return type is T.
It is a compile-time error if an argument to a class instance creation expression is not compatible with its target type, as derived from the invocation type (§15.12.2.6).
If the compile-time declaration is applicable by variable arity invocation (§15.12.2.4), then where the last formal parameter type of the invocation type of the constructor is F_n[], it is a compile-time error if the type which is the erasure of F_n is not accessible at the point of invocation.
The type of the class instance creation expression is the return type of the chosen constructor, as defined above.
Note that the type of the class instance creation expression may be an anonymous class type, in which case the constructor being invoked is an anonymous constructor (§15.9.5.1).