Skip to content
Preptima
mediumConceptDesignMidSeniorStaff

How do records, sealed types and pattern matching change the way you model a domain?

Together they let you write closed data hierarchies the compiler can check. A record declares that a type is its components, a sealed interface enumerates the permitted variants, and an exhaustive switch over record patterns turns adding a variant into a compile error at every place that handles them.

5 min readUpdated 2026-08-15

Answer

Short answer

Java records and sealed types make domain modelling more explicit by separating immutable data carriers from closed sets of variants. Records give compact value-style data classes, sealed interfaces bound the permitted subtypes, and pattern matching lets the compiler help when a new variant must be handled.

Was this answer helpful?

Likely follow-ups

  • Why can a record not have instance fields beyond its components?
  • When is a sealed hierarchy plus a switch worse than an abstract method on the interface?
  • What happens to an exhaustive switch when the sealed interface gains a permitted subtype and you do not recompile the consumer?
  • Why do records make awkward JPA entities?

Related questions

Further reading