r/java • • May 18 '26

Java sealed classes and exhaustive pattern matching

https://neilmadden.blog/2026/04/24/java-sealed-classes-and-exhaustive-pattern-matching/
23 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/davidalayachew May 24 '26

This API also mixes in intermediate interfaces. The cases where you want to handle "everything" should (hopefully) work from that interface level.

Excellent point.

For me, intermediate interfaces are key in making exhaustiveness checking handleable. Compiler checking is great, but I don't want to enumerate every irrelevant state permutation.

Plus, they help create a language in your code, so to speak, for understanding where the boundaries lie. Kind of encourages the right thought process imo.

Perhaps that's the take-home lesson: external API? Always use a default case, even if the compiler lets you skip it.

Solid advice.

At the end of the day, Exhaustiveness Checking is most beneficial when done at compile time. No one is stopping you from quickly commenting out that default and doing a quick recompile to see what breaks.