# Java's sealed classes can break your API without a compile error

Original title: Java sealed classes and exhaustive pattern matching

> Hacker News story 50141579 · language: en · submitted 2026-10-18
> Web version: https://zeli.app/story/50141579
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 184 · by [hnisjafx40](<https://news.ycombinator.com/user?id=hnisjafx40>) · 💬 145
[original](<https://neilmadden.blog/2026/04/24/java-sealed-classes-and-exhaustive-pattern-matching/>) · [HN discussion](<https://news.ycombinator.com/item?id=50141579>)

## Summary

Java 17's sealed classes enable exhaustive pattern matching, letting the compiler verify that all subtypes are handled. But adding a new subtype to a sealed interface is a breaking change for consumers, and if they aren't recompiled, the error only surfaces at runtime as a MatchException. The author concludes sealed types work best inside a component, not in public APIs, and points to a JDK proposal that makes exactly this mistake.

## Key quote

> So even if you were hoping that using sealed classes would statically ensure that you update all consumers when a new case is added, this is not the case unless you recompile everything.

## HN discussion (145 comments)

### [shepherdjerred](<https://news.ycombinator.com/item?id=50144140>)

> I love how far Java has come. I still prefer TypeScript since I do a lot of frontend work and prefer to use the same language across the stack.
>
> With the said, you can do exhaustive pattern matching in TypeScript! https://github.com/gvergnaud/ts-pattern

### [armchairhacker](<https://news.ycombinator.com/item?id=50142099>)

> Java also has recursive patterns and (maybe still in preview) literal patterns: `switch (x) { case A(B(true, var c)) -> f(c); }`

### [another_twist](<https://news.ycombinator.com/item?id=50146010>)

> Took me a while to get used to sealed and I didn't quite see the use. But AI seems to love it for Java so I gave it a go and now I get it. Its like a really good access control mechanism esp if you use DI and want to deprecated some stuff. Apart from ofcourse the pattern matching - which comes in handy for event driven messages where payload shape changes.

### [timshel2](<https://news.ycombinator.com/item?id=50142681>)

> Scala punching the air rn

### [panny](<https://news.ycombinator.com/item?id=50145426>)

> I don't think you need those switch defaults with the sealed types. I didn't check with a compiler, but I think those are unnecessary.

### [Larrikin](<https://news.ycombinator.com/item?id=50142106>)

> Java continues to try and catch up to Kotlin
>
> https://kotlinlang.org/docs/sealed-classes.html
>
> I didn't mind Java when the comparisons were to C++, Ruby, and Python. I can not find a reason to go back after Scala (which seemed to fail due to being indiscriminate) and especially Kotlin

---

## Cite

- Page: https://zeli.app/story/50141579
- Date: 2026-10-18
- Source: neilmadden.blog (https://neilmadden.blog/2026/04/24/java-sealed-classes-and-exhaustive-pattern-matching/)
- Hacker News: https://news.ycombinator.com/item?id=50141579

## Other languages

- zh: https://zeli.app/zh/story/50141579 · markdown: https://zeli.app/zh/story/50141579.md
- ja: https://zeli.app/ja/story/50141579 · markdown: https://zeli.app/ja/story/50141579.md
- ko: https://zeli.app/ko/story/50141579 · markdown: https://zeli.app/ko/story/50141579.md
- de: https://zeli.app/de/story/50141579 · markdown: https://zeli.app/de/story/50141579.md
- es: https://zeli.app/es/story/50141579 · markdown: https://zeli.app/es/story/50141579.md
- ru: https://zeli.app/ru/story/50141579 · markdown: https://zeli.app/ru/story/50141579.md
