Object instantiation creates a runtime object from a class or object definition. In object-oriented programming, straightforward construction becomes difficult when many optional parameters hide each argument’s meaning. section 4.1.2
When positional construction loses intent
A Person created with ten positional values and several nulls forces readers to remember parameter order and decode every placeholder. Overloaded constructors avoid some nulls but produce a telescoping constructor: covering every optional combination can require up to 1,024 constructors for ten fields. section 4.1.2
Person p = Person.builder().firstname("John")
.lastname("Doe")
.town("Grenoble")
.build();
A builder expresses selected fields as named actions, making flexible instantiation easier to read than a long positional constructor. section 4.1.2
Use these practical distinctions when designing object creation:
- Prefer a Builder pattern when many parameters are optional and positional construction obscures intent. section 4.1.2
- Treat encapsulation as a tradeoff: protecting consistency can add constructors, accessors, equality logic, string representations, and builders. section 4.1.2
