liveBook · Manning

Object instantiation

As explained in The Art of Code

Object instantiation is the creation of a runtime object from a class or object definition.

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

How The Art of Code covers it

How deep it goes
Contrasts positional constructors, telescoping overloads, and builders, but does not develop the builder implementation in full.
Examples
Uses a ten-field Person object, null-filled positional arguments, overloaded constructors, and fluent builder calls.
What it assumes
Assumes familiarity with object-oriented classes, constructors, encapsulation, and optional parameters.
What you can do after
Apply the Builder pattern to make multi-parameter object creation more readable and flexible.
How it connects
Connects object instantiation with encapsulation, constructors, accessors, and the Builder pattern.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.