A missing value is Java’s null reference: it signals the absence of an object, not an instance of the declared type. That makes a field typed String less trustworthy than it looks—unless the model states whether absence is allowed. In data-oriented code, optionality is domain information, not an implicit escape hatch. section 2.6.3
Consider a procedure step that records who completed it and when. Those facts are meaningful only after completion. Adding nullable completedBy and completedOn fields to one Status record makes its meaning depend on combinations of a boolean and nulls. It also permits impossible combinations: an incomplete step with completion details, or a completed step without them. section 4.1.4
Defend a required value at construction
For data that must always exist, reject null at the boundary. A compact constructor makes the requirement visible and prevents a record that is structurally populated but semantically empty. record Person(String firstName, String lastName) { Person { Objects.requireNonNull(firstName); Objects.requireNonNull(lastName); } } section 2.6.3
Constructor checks are a practical safeguard, especially while refactoring code shaped by older habits. They can harden known invariants and stop invalid data from propagating. But checks around a nullable field and a status flag are also evidence that the representation is carrying several states whose distinctions should be made explicit. section 4.1.4 section 9.3.1
Use missing values deliberately rather than as unspoken state:
- Treat a non-null field as a domain claim: construction must provide the value. section 2.6.3
- Identify fields whose relevance changes with lifecycle state; do not let null combinations define that lifecycle implicitly. section 4.1.4
- During a transition, explicitly reject nulls where an invariant requires presence instead of relying only on team convention. section 9.3.1
Listing 2.44 defending against nulls in the constructor
record Person(String firstName, String lastName) {
Person {
Objects.requireNonNull(firstName); #A
Objects.requireNonNull(lastName); #A
}
}
Person person = new Person(null, null); // ERROR #BThis compact constructor turns required names into an executable rule: attempts to construct Person with null values fail immediately. section 2.6.3






































