Course contentsShow
← Java
Lesson 1752 of 2,87036. Lambda Expressions and Method ReferencesFree lesson

Constructor Overloading Resolution

Understand how the compiler selects the correct constructor based on the target functional interface signature.

Constructor Overloading Resolution

You'll learn: How the Java compiler chooses the right constructor when multiple overloaded constructors match a constructor reference's functional interface signature.

When you use a constructor reference like MyClass::new, the compiler must decide which constructor to invoke if MyClass has multiple overloaded constructors. The selection process relies on the target type — specifically, the parameter list of the functional interface's abstract method.

How the Compiler Decides

The compiler examines the functional interface you're assigning the constructor reference to. It looks at the number and types of parameters in the interface's method signature, then matches them to a compatible constructor.

class Person {
    Person() { }
    Person(String name) { }
    Person(String name, int age) { }
}

// Target: Supplier<Person> — no parameters
Supplier<Person> s = Person::new;  // Calls Person()

// Target: Function<String, Person> — one String parameter
Function<String, Person> f = Person::new;  // Calls Person(String)

// Target: BiFunction<String, Integer, Person> — two parameters
BiFunction<String, Integer, Person> bf = Person::new;  // Calls Person(String, int)

Resolution Rules

The compiler uses the same overload resolution rules as regular method calls:

  1. Match parameter count from the functional interface method
  2. Match parameter types (exact match or compatible through widening/boxing)
  3. If multiple constructors match, the most specific one wins
  4. If ambiguity remains, compilation fails

Think of it like ordering at a restaurant: you specify what you want (the functional interface signature), and the kitchen delivers the exact dish (constructor) that matches your order.

Key Takeaway:
The compiler selects which constructor to invoke based on the target functional interface's method signature — matching parameter count and types to the appropriate constructor overload.