Skip to content
Back to skills

Java Best Practices

ASecurity

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)

  • 67 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 10, 2026
developmentgojavarefactoringapi

Works with

  • api

Security analysis

A100/100

Scanned November 10, 2026

npx -y skills add ComeOnOliver/skillshub --skill java-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Java Best Practices?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Java Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/comeonoliver-java-best-practices/badge)](https://www.skillsdirectory.com/skills/comeonoliver-java-best-practices)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: java-best-practices
description: "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)"
---

# 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)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.