Choosing the Constructor and its Arguments

15.9.3 Choosing the Constructor and its Arguments

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, and the direct superclass of C , S , is an inner class, then:

◆ If S is a local class and S occurs in a static context, then the arguments in the argument list, if any, are the arguments to the constructor, in the order they

appear in the expression. ◆ Otherwise, the immediately enclosing instance of i with respect to S is the

first argument to the constructor, followed by the arguments in the argument list of the class instance creation expression, if any, in the order they appear in the expression.

• Otherwise the arguments in the argument list, if any, are the arguments to the constructor, in the order they appear in the expression.

15.9.3 Choosing the Constructor and its Arguments EXPRESSIONS

Second, a constructor of C and corresponding result type and throws clause are determined:

• If the class instance creation expression uses " <> " to elide class type arguments,

a list of methods m 1 ... m n is defined for the purpose of overload resolution and type argument inference.

Let c 1 ... c n be the constructors of class C . Let #m be an automatically generated name that is distinct from all constructor and method names in C . For all j (1 ≤

j ≤ n), m j is defined in terms of c j as 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 j are those of c j .

◆ The type parameters of m j are 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 or c j .

◆ The return type of m j is θ j applied to C <F 1 ,..., F p > .

◆ The name of m j is #m . ◆ The (possibly empty) list of argument types of m j is θ j applied to the argument

types of c j . ◆ The (possibly empty) list of thrown types of m j is θ j applied to the thrown types of c j . ◆ The body of m j is irrelevant.

To choose a constructor, we temporarily consider m 1 ... m n to be members of C . Then one of m 1 ... m n is selected, as determined by the class instance creation's argument expressions, using the process described in §15.12.2.

It is a compile-time error if there is no unique most-specific method that is both applicable and accessible.

Otherwise, where m j is the selected method, c j is the chosen constructor. The result type and throws clause of c j are the same as the return type and throws clause determined for m j (§15.12.2.6).

• Otherwise, the class instance creation expression does not use " <> " to elide class type arguments.

EXPRESSIONS Run-time Evaluation of Class Instance Creation Expressions 15.9.4

Let T

be the type denoted by C followed by any class type arguments in the expression. The process described in §15.12.2, modified to handle constructors, is used to select one of the constructors of T and determine its throws clause.

As in method invocations, it is a compile-time error if there is no unique most- specific constructor that is both applicable and accessible.

Otherwise, the result type is T . Finally, the type of the class instance creation expression is the result 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).