I've never heard about consistent constructors but I guess it's about parameters initialized by defferent constructors. Let's see how inconsistent constructors could look like:
class Person {
String firstName;
String lastName;
Integer age;
Person(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
Person(String firstName, String lastName) {
this(firstName, lastName, 0);
}
Person(String firstName) {
this(firstName, null, null);
}
}
Person p1 = new Person("Joe", "Doe", 24);
Person p2 = new Person("Joe", "Doe");
Person p3 = new Person("Joe");
Where is the inconsistency? You'd expect that age of person number #2 and #3 (p2
, p3
) would be undefined, i.e. its value would be -1
, 0
or null
. It doesn't matter which one as long as using Person(String, String)
and Person(String
would give you the very same, consistent, value.
In this example second construcot initialize age
to 0
, but third constructor initialize this property to null
- that seems to be quite inconsistent.