# Java Best Practices

> Core Effective Java patterns for robust, maintainable Java code. Use when applying SOLID principles, choosing between inheritance and composition, refactoring Java code smells, or reviewing class design. (triggers: **/*.java, refactor, SOLID, builder, factory, composition, immutable, Optional, checked exception, clean code)

- Skill: `comeonoliver/java-best-practices` (Agent Skill)
- Install (CLI): `npx skillmds@latest add comeonoliver/java-best-practices`
- Raw SKILL.md: https://api.skillmd.com/api/skills/comeonoliver/java-best-practices/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: ComeOnOliver (https://skillmd.com/u/comeonoliver)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/comeonoliver/java-best-practices

---


# Java Best Practices

## **Priority: P1 (HIGH)**

Core engineering principles for robust, maintainable Java systems.

## Implementation Guidelines

- **Immutability**: Prefer immutable objects (`final` fields, unmodifiable collections).
- **Access Modifiers**: Minimize visibility. Default to **package-private** (no modifier). Use `private` for all fields. Only `public` for API contracts.
- **Composition > Inheritance**: Favor `Has-A` over `Is-A`. Avoid deep hierarchies.
- **Constructors**: Use Static Factory Methods (`User.of()`) over complex constructors.
- **Builder Pattern**: Use for objects with 4+ parameters.
- **Exceptions**: Recoverable → Checked; Programming error → Unchecked.
- **Fail Fast**: Validate parameters (`Objects.requireNonNull`) at the method start.
- **Interfaces**: Code to interfaces (`List`, `Map`), not implementations (`ArrayList`).
- **Dependency Injection**: Inject dependencies via constructor; don't create them internally.
- **Method References**: Use `String::toUpperCase` over `s -> s.toUpperCase()` where readable.

## Anti-Patterns

- **No Null Returns**: Return Optional<T> or empty collection instead.
- **No Empty Catch**: Log or rethrow; never swallow exceptions silently.
- **No God Class**: Split into focused classes following Single Responsibility Principle.
- **No Magic Numbers**: Extract named constants with clear meaning.
- **No Mutable Statics**: Avoid public static mutable fields (global state).

## References

- [Static Factory & Composition Examples](references/example.md)


