JVM Knowledge

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 i with 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 private inner 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-private inner member class, then the first argument to the constructor is the immediately enclosing instance of i (§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 throws clause and return type are determined:

  • If the class instance creation expression does not use <>, then:

    • If C is not an anonymous class, then:

      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 choose one of the constructors of T and determine its throws clause.

      If there is no unique most-specific constructor in T that is both applicable and accessible (§6.6), then a compile-time error occurs (as in method invocations).

      Otherwise, the return type corresponding to the chosen constructor is T.

    • If C is an anonymous class, then:

      The process specified in §15.12.2, modified to handle constructors, is used to choose one of the constructors of the direct superclass type of C and determine its throws clause.

      If there is no unique most-specific constructor in the direct superclass type of C that is both applicable and accessible, then a compile-time error occurs (as in method invocations).

      Otherwise, C's anonymous constructor is chosen as the constructor of C (§15.9.5.1). Its body consists of an explicit constructor invocation (§8.8.7.1) of the constructor chosen in the direct superclass type of C.

      The throws clause of the chosen constructor includes the exceptions in the throws clause of the constructor chosen in the direct superclass type of C.

      The return type corresponding to the chosen constructor is the anonymous class type.

  • If the class instance creation expression uses <>, then:

    If C is not an anonymous class, let D be the same as C. If C is an anonymous class, let D be the superclass or superinterface of C named by the class instance creation expression.

    If D is a class, let c_1, ..., c_n be the constructors of class D. If D is an interface, let c_1, ..., c_n be a singleton list (n = 1) containing the zero-argument constructor of the class Object.

    A list of methods m_1, ..., m_n is defined for the purpose of overload resolution and type argument inference. 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 D, 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 D.

      θ_j is [F_1:=X_1, ..., F_p:=X_p, G_1:=Y_1, ..., G_q:=Y_q].

    • The type parameters of m_j are X_1, ..., X_p, Y_1, ..., Y_q. The bound of each type parameter, if any, is θ_j applied to the corresponding type parameter bound in D or c_j.

    • The return type of m_j is θ_j applied to D<F_1, ..., F_p>.

    • 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 modifiers of m_j are those of c_j.

    • The name of m_j is #m, an automatically generated name that is distinct from all constructor and method names in D and is shared by m_1, ..., m_n.

    • The body of m_j is irrelevant.

    To choose a constructor, we temporarily consider m_1, ..., m_n to be members of D. One of m_1, ..., m_n is chosen, as determined by the class instance creation expression'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_j is the chosen method:

    • If C is not an anonymous class, then c_j is chosen as the constructor of C.

      The throws clause of the chosen constructor is the same as the throws clause determined for m_j.

      The return type corresponding to the chosen constructor is the return type determined for m_j (§15.12.2.6).

    • If C is an anonymous class, then C's anonymous constructor is chosen as the constructor of C. Its body consists of an explicit constructor invocation (§8.8.7.1) of c_j.

      The throws clause of the chosen constructor includes the exceptions in the throws clause determined for m_j.

      The return type corresponding to the chosen constructor is the anonymous class type.

    If the class instance creation expression is a poly expression, then its compatibility with a target type is as determined by §18.5.2.1, using m_j as the selected method m.

    Testing for compatibility with a target type may occur multiple times before making a final determination of the class instance creation expression's target type and the return type corresponding to the chosen constructor. For example, an enclosing method invocation expression may require testing the class instance creation expression for compatibility with different methods' formal parameter types.

    If C is an anonymous class, then its direct superclass type or direct superinterface type is the return type determined for m_j (§15.12.2.6).

    It is a compile-time error if the direct superclass type or direct superinterface type, or any subexpression therein ("subexpression" includes type arguments of parameterized types, bounds of wildcard type arguments, and element types of array types, but excludes bounds of type variables), has one of the following forms:

    • A type variable that was not declared as a type parameter (such as a type variable produced by capture conversion).
    • An intersection type.
    • A class or interface type, where the class or interface declaration is not accessible from the class or interface in which the class instance creation expression appears.

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 corresponding to the chosen constructor, as defined above.