Use a sealed interface to make the allowed variants compiler-checked: ```java public sealed interface Payment permits Card, Cash, Voucher { } public record Card(String last4) implements Payment { } public record Cash() implements Payment { } public record Voucher(String code) implements Payment { } ``` Java 21 can then use an exhaustive pattern-matching switch: ```java static String describe(Payment payment) { return switch (payment) { case Card card -> "Card ending in " + card.last4(); case ...
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill java-language --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Java Language?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-java-language-6d799948)More formats (shields.io, HTML) on the badges page.
Use a sealed interface to make the allowed variants compiler-checked:
```java
public sealed interface Payment
permits Card, Cash, Voucher {
}
public record Card(String last4) implements Payment {
}
public record Cash() implements Payment {
}
public record Voucher(String code) implements Payment {
}
```
Java 21 can then use an exhaustive pattern-matching switch:
```java
static String describe(Payment payment) {
return switch (payment) {
case Card card -> "Card ending in " + card.last4();
case Cash cash -> "Cash";
case Voucher voucher -> "Voucher " + voucher.code();
};
}
```
Because `Payment` is sealed, no other implementation can be passed as a `Payment`, and the compiler verifies that the switch handles every permitted variant. If the variants are only labels with no data or behavior, an `enum Payment { CARD, CASH, VOUCHER }` is the simpler choice.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!