I'm reading up Joshua Bloch's 'Effective Java' where in Item 2, he mentions the advantages of using Builder pattern when dealing with several parameters in the constructor. All's good, until I saw the multiple var-args difference between the conventional constructor and this pattern. So, I have some doubts regarding it :
I haven't used var-args in my code though, but yes I know their use. Still I'm unable to understand the reason behind above statements. Any help would be appreciated.
No method signature (constructors included) allows for multiple varargs. There can be only one, and it has to be the last argument.
That is just a limitation in the language specification. And yes, the reason for that is likely that it can become ambiguous very fast if you allowed more flexibility.
In the builder pattern, there is no such limitation, as every parameter can get its own method.
builder
.withOptions("a", "b", "c") // varargs
.withColors("red", "blue") // more varargs
.build();
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