dimanche 27 décembre 2015

Varargs-Constructor cannot act as default constructor using reflection

FXML-View:

....
    <GridPane...>
        <PersonController... />
    </GridPane...>
....

Class PersonController:

public PersonController(Person... personsToExclude) {
    FXMLLoader.load("PersonControllerView.fxml")
    this.personsToExclude = personsToExclude;
}

This code sample leads to an excpetion, because the class cannot be invoked without a default constructor (by the FXMLLoader). Now my question: You CAN use this constructor as default constructor. You can create such an object like this:

PersonConstructor pc = new PersonConstructor(); // This calls the upper constructor

Why isn't reflection able to use this constructor too? Varargs appear to be arrays internally which will be null by default if no parameter was handed over.

Was this design decision solely made to reduce complexity (it actually does reduce it a little bit for the reader) or are there any other reasons why it is important to still have a "real" default constructor?





Aucun commentaire:

Enregistrer un commentaire