---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 15. Expressions"
section: "15.9.3"
heading: "15.9.3. Choosing the Constructor and its Arguments"
parent: "15.9. Class Instance Creation Expressions"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.9.3
id: "15.9.3"
url: https://jvm.obfuscated.software/jls/se8/15.9.3.html
raw: https://jvm.obfuscated.software/jls/se8/15.9.3.md
---

# 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 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](15.9.2.md)), 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](8.8.1.md), [§15.9.2](15.9.2.md)), 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 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 specified in [§15.12.2](15.12.2.md).

  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 selected method, `c_j` is the chosen constructor. The return 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](15.12.2.6.md)).

- 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](15.12.2.md), modified to handle constructors, is used to select one of the constructors of T and determine its `throws` clause.

  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](15.12.2.6.md)).

If the compile-time declaration is applicable by variable arity invocation ([§15.12.2.4](15.12.2.4.md)), 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](15.9.5.1.md)).
