Open Zegveld opened 11 months ago
When you have multiple constructors. The smaller ones usually have default values set for other fields, while the most extensive one has all options.
For example:
class MyClass { private final String defaultValue; private final String requiredValue; MyClass(String requiredValue) { this(requiredValue, "defaultValue"); } MyClass(String requiredValue, defaultValue) { this.requiredValue = requiredValue; this.defaultValue = defaultValue; } // getters }
If you use a builder like this:
new MyClassBuilder().withRequiredValue("value").build();
then the wanted constructor would be MyClass(requiredValue)
MyClass(requiredValue)
However if you use a builder like this:
new MyClassBuilder().withRequiredValue("value").defaultValue("value").build();
then the wanted constructor would be MyClass(requiredValue, defaultValue)
MyClass(requiredValue, defaultValue)
So the builder should pick the correct constructor based on the values that are set in the builder.
When you have multiple constructors. The smaller ones usually have default values set for other fields, while the most extensive one has all options.
For example:
If you use a builder like this:
then the wanted constructor would be
MyClass(requiredValue)
However if you use a builder like this:
then the wanted constructor would be
MyClass(requiredValue, defaultValue)
So the builder should pick the correct constructor based on the values that are set in the builder.