javalisted
Install: claude install-skill dean0x/devflow
# Java Patterns
Reference for modern Java patterns, type system, and best practices.
## Iron Law
> **FAVOR COMPOSITION OVER INHERITANCE**
>
> Delegation and interfaces over class hierarchies. Inheritance creates tight coupling,
> breaks encapsulation, and makes refactoring dangerous. Use interfaces for polymorphism,
> records for data, and sealed classes for restricted hierarchies. Extend only when the
> "is-a" relationship is genuinely invariant. — Effective Java, Item 18 [1, Item 18]
## When This Skill Activates
- Working with Java codebases (records, sealed classes, Optional, streams)
- Implementing concurrent code or API design
- Structuring Java packages and dependencies
---
## Type System (Modern Java)
**Records for data** [4][9] — Immutable carriers with auto-generated `equals`/`hashCode`/
`toString`. Compact constructors validate before fields are sealed.
```java
// BAD: Mutable POJO. GOOD: Immutable record [1, Item 17][4]
public record User(String name, String email, Instant createdAt) {
public User {
Objects.requireNonNull(name); Objects.requireNonNull(email);
}
}
```
**Sealed classes** [5][9] — Close the subtype set, enabling exhaustive pattern matching. [6]
```java
public sealed interface Result<T> permits Success, Failure {
record Success<T>(T value) implements Result<T> {}
record Failure<T>(String error) implements Result<T> {}
}
// Exhaustive — no default needed [6]
switch (result) {
case Success<User> s -> handleSuccess(s