I am not sure if a similar question was asked before, they searched for it, but did not receive useful answers.
How does the question arise, which is better, having an overloaded constructor or having several setter functions?
Scenario:
public class Something {
private int a;
private int b;
public Something(int a, int b) {
this.a = a;
this.b = b;
}
...
}
Now my main requirement was to have only two parameters. Now tomorrow the requirement will be changed, and I will be asked to add a new parameter c, and then the next day d and give the statement that we can have more fields.
I already have a dependency for this constructor in several projects. Now back to my question
- Is it advisable to add new fields to an already overloaded constructor?
- Create a new overloaded constructor every time I need to add a new field so that I don't break the dependent code?
- ( , ).
, ?