23

Where Java 25 retires classic idioms

The modern forms that replace double-checked locking, hand-written value classes, double-dispatch Visitor, and the null check.

Demo 23Source on GitHub ↗

The book’s through-line, gathered as working code: the modern Java 25 forms that make four classic idioms unnecessary. An enum singleton (proven under a 32-thread race) replaces double-checked locking; a record replaces a hand-written value class and its defensive copies; a sealed interface plus exhaustive switch replaces the double-dispatch Visitor and the instanceof chain; Optional replaces the null check. The retired idioms are described in the chapter prose; this module ships only the modern forms, with tests.

Build, run, observe

cd examples/23-idiom-retirement
mvn -q test
./demo.sh

The module also runs from the aggregator root with mvn -q -f examples/pom.xml test.