I have a PersonFactory
interface as follows:
@FunctionalInterface
public interface PersonFactory<P extends Person> {
P create(String firstname, String lastname);
// Return a person with no args
default P create() {
// Is there a way I could make this work?
}
}
The Person
class:
public class Person {
public String firstname;
public String lastname;
public Person() {}
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
I want to be able to instantiate my Person
s like this:
PersonFactory<Person> personFactory = Person::new;
Person p = personFactory.create(); // does not work
Person p = personFactory.create("firstname", "lastname"); // works
Is there a way I could make the Java compiler automatically choose the right constructor by matching the signature of PersonFactory.create()
?
One way would be to have the following:
But I'm not sure that's what you wanted. The problem is that you can't make a method reference refer to 2 different methods (or constructors). In this case, you want
Person::new
to refer to the constructor taking no parameters and the constructor taking 2 parameters, which is not possible.When you have:
and use it like
you have to realize that the method-reference
Person::new
refers to the constructor taking 2 parameters. The next line just invokes it by passing the parameters.You could also write it more explicitely using a lambda expression: