Reputation: 183
I have a class in Java. This class has a constructor like below:
public Person(String first, String last) {
firstName = new SimpleStringProperty(first);
lastName = new SimpleStringProperty(last);
city = new SimpleStringProperty("Tehran");
street = new SimpleStringProperty("Vali Asr");
postalCode = new SimpleStringProperty("23456");
birthday = new SimpleStringProperty("12345");
}
Now I want to declare a constructor like below:
public Person() {
Person(null, null);
}
But it gives me an error. What can I do? Thanks
Upvotes: 1
Views: 122
Reputation: 44965
Calling Person(null, null)
doesn't work because a constructor is not a method so it cannot be called like a method as you are trying to do.
What you need to do is rather to call this(null, null)
to invoke the other constructor as next:
public Person() {
this(null, null); // this(...) like super(...) is only allowed if it is
// the first instruction of your constructor's body
}
public Person(String first, String last) {
...
}
More details about the keyword this
here.
Upvotes: 10