Enums on steroids - let's look at sealed classes!
summary
Claim: sealed classes plus records and pattern matching replace enums to model mutually exclusive outcomes. Implementation uses Java 17+ features: a sealed Response interface with three permitted records Approval, Refusal, Suspension and a pattern‑matching switch. Built a loan evaluator returning three concrete types (three surprising benefits: no instanceof casts, no default branch, and simpler tests). Pattern matching is still a preview feature so Maven/Gradle flags are required to compile, but the example compiles and runs and removes the previous response class smell.
AI-written summary · about
chapters (13)
- 0:00Introduction
- 1:51Problem with enums
- 3:15First solution attempt
- 5:41Code smell in response class
- 7:29Introducing class hierarchy
- 8:54Using records for immutable data
- 9:35Pattern matching in Java
- 10:38Preview feature disclaimer
- 11:22Improved loan application
- 12:06Sealed classes introduction
- 13:54Final solution with sealed classes
- 15:41Testing improvements
- 16:04GitHub repository and Q&A
related talks
38mEnums on steroids - let's look at sealed classes !
43mEnums on steroids - let's look at sealed classes!
01h 02mThe Three Amigos: Intermixing Pattern Matching, Records, and Sealed Classes
01h 01mDesigning with Sealed Classes
56mThe Art of Java Language Pattern Matching
01h 08mClean Application Development with Records, Sealed Classes and Pattern Matching
39mPattern Matching: Small Enhancement or Major Feature?
46mThe Art of Java Type Patterns
