I have a number of set methods setup and using a constructor to initiate their value as follows. It is working but I am concerned as to whether it is a normal Java convention to do so (is it normal to call a set method inside a constructor?).
The alternative way I can think of is to call an empty constructor and call the set methods one by one in the main method to set the value. I find this second method tedious. Please advice if what I have done is fine/ within Java convention or if there is a better way to do it. Do tell if I need to post more code to make my question more clearer. I can post it all if it would make better sense. Thank you.
public Person(String foreName,String surName, int age, double height, String gender){
personCount++;
setForeName(foreName);
setSurName(surName);
setAge(age);
setHeight(height);
setGender(gender);
}
If using this
doesn't bypass the validation rules defined in the setters, it's just a matter of taste:
public Person(String foreName,String surName, int age, double height, String gender){
personCount++;
this.foreName = foreName;
//same goes for the rest of the params
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With