Lesson 1 of 9 · 23m
Records Are Domain Values, Not Just Less Boilerplate
Model immutable value objects with records, compact constructors, validation, and a clear sense of when records are the wrong tool.
The first thing modern Java teaches an old Java hand is humility: most of the equals, hashCode, toString, getter, and constructor code we spent years generating was not craftsmanship. It was compensation for missing language support. Records fix that, but the real win is not fewer lines. The real win is that a value object finally says what it means.
The mental model
A record is a transparent carrier for immutable data. Transparent means its state is visible through its accessors; carrier means it does not hide behavior behind ceremony; immutable means every component is final.
public record Money(BigDecimal amount, Currency currency) {
public Money {
Objects.requireNonNull(amount, "amount");
Objects.requireNonNull(currency, "currency");
amount = amount.stripTrailingZeros();
}
}That compact constructor is the validation gate. Assignment to amount inside it changes the value eventually assigned to the field; you do not assign fields yourself. If that sentence feels odd, good — records reward learning the exact mechanism instead of pretending they are ordinary classes.
Use them at boundaries
Records shine where data crosses a trust boundary: API payloads, commands, events, configuration slices, database projections, cache keys, and results.
public record CreateInvoiceRequest(
UUID customerId,
List<InvoiceLine> lines,
IdempotencyKey idempotencyKey
) {}This is documentation the compiler checks. A method returning InvoiceSummary is clearer than one returning Object[], a tuple-shaped JSON node, or a mutable bean whose population depends on which service touched it last.
Do not confuse records with entities
A customer entity has identity, lifecycle, invariants, and mutable state. A CustomerSnapshot or CustomerCreated event may be a record. A JPA entity usually should not be: proxies, lazy loading, reflection frameworks, bidirectional relationships, and mutation do not fit the transparent immutable-carrier model.
Keep entities for persistence if your stack requires them. Use records as the language of the domain and API around those entities.
The details that bite
Accessors are named after components, not JavaBeans conventions: money.amount(), not money.getAmount(). Some libraries now understand records well; older serialization and mapping frameworks may need configuration. Generic type erasure still exists. Deep immutability does not exist either — a record holding a mutable List holds a mutable list.
public record Batch(String name, List<String> entries) {
public Batch {
entries = List.copyOf(entries);
}
}List.copyOf rejects null elements and makes a defensive immutable copy. That is usually closer to the invariant you intended than copying with new ArrayList.
Field notes
- Records are shallowly immutable by default; copy mutable components before assigning them.
- Override the compact constructor for validation and normalization, not for hiding all behavior.
- A record can implement interfaces, including sealed interfaces, but cannot extend another class.
- Do not use records as a dumping ground for unrelated fields because constructors became cheap.
- Jackson handles records well on modern versions; ancient binding libraries often silently lose components.
Was this lesson helpful?