# Java Conventions

> Java code conventions covering Java 17+ records and sealed classes, formatting/static analysis, Maven/Gradle, JUnit 5, exception handling, constructor injection, streams, and security. Load when writing or reviewing Java code.

- Skill: `goldziher/java-conventions` (Agent Skill)
- Install (CLI): `npx skillmds@latest add goldziher/java-conventions`
- Raw SKILL.md: https://api.skillmd.com/api/skills/goldziher/java-conventions/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: goldziher (https://skillmd.com/u/goldziher)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/goldziher/java-conventions

---


- Java 17+ LTS, records for immutable data, sealed classes for restricted hierarchies, pattern matching.
- A consistent style guide and auto-formatter (e.g., Google Java Style + google-java-format). Static analysis (e.g., Error Prone + SpotBugs).
- Build: Maven or Gradle with wrapper scripts (`mvnw`/`gradlew`). Commit wrapper files.
- Testing: JUnit 5 with `@ParameterizedTest`, an assertion library (e.g., AssertJ), a coverage tool (e.g., JaCoCo) at 80%+.
- Benchmarking: JMH for microbenchmarks — never use `System.nanoTime()` loops.
- Error handling: specific exceptions only, never `catch (Exception)`, use try-with-resources for `AutoCloseable`.
- `var` for obvious types, `Optional<T>` for returns (never params/fields), `final` fields by default.
- Prefer constructor injection over field injection for DI. Immutable collections: `List.of()`, `Map.of()`.
- Streams for collection transforms, `instanceof` pattern matching, switch expressions.
- Security: OWASP `dependency-check` Maven/Gradle plugin for CVE scanning.
- Dependencies: commit lock files, use BOM for version alignment, avoid `SNAPSHOT` in releases.
- Anti-patterns: public fields, mutable static state, raw types, checked exceptions in lambdas.

