liveBook · Manning

Compact constructor

As explained in Data-Oriented Programming in Java

A compact constructor is a Java record constructor form that omits the explicit parameter list and field assignments while allowing constructor-body validation and transformation of component values.

A compact constructor is a record constructor that removes the repeated parameter list and field assignments, leaving the body focused on what makes the data valid. The record header supplies the components, and the runtime handles the routine assignment. That makes the form especially useful when a record needs semantic guardrails without traditional constructor ceremony. section 2.6.1

Reject invalid data

A Rational record can enforce its basic invariant at construction time: reject a zero denominator with an IllegalArgumentException. The constructor protects the value’s meaning before invalid data enters the program. section 2.6.1

Normalize incoming values

Validation is only one use. A Rational compact constructor can calculate a greatest common divisor and divide both components by it, transforming equivalent inputs into a simplified representation before assignment. section 2.6.1

Use compact constructors to:

  • Enforce what a data type means, rather than accumulate unrelated business rules. section 2.6.1
  • Transform incoming arguments when the representation must be simplified or protected from unexpected mutability. section 2.6.1

How Data-Oriented Programming in Java covers it

How deep it goes
Explains syntax, invariants, and argument transformation, while deferring broader questions about data representation.
Examples
Demonstrates denominator validation and greatest-common-divisor normalization in a Rational record.
What it assumes
Assumes familiarity with Java records and value-oriented modeling while teaching compact-constructor validation from its purpose.
What you can do after
Build it from scratch by defining record invariants and transforming constructor arguments before assignment.
How it connects
Connects records with value classes, data semantics, invariants, immutability, and data-oriented programming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.