Why does a single constructor with many parameters, some of them optional, become a real risk rather than just an inconvenience?
Once a constructor has several parameters of the same type, several booleans in a row, for example, nothing stops a caller from passing them in the wrong order, and the mistake compiles perfectly fine while silently constructing the wrong object. There is also no clean way to support 'give me an object using only the mandatory fields, defaulted otherwise' without either a second constructor whose parameter types happen to differ, or writing a constructor for every meaningful combination of optional fields present or absent, which can approach two-to-the-power-of-n constructors for n optional parameters. Builder fixes both problems: each field is set by a clearly named method rather than by position, so a swapped argument cannot happen, and any optional field can simply be skipped, with a sensible default applied automatically.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response