Java Style Guide¶
Status: 🟢 Active | Owner: Java Guild
Base Standard¶
Google Java Style Guide is the base standard with the following enterprise overrides.
Enterprise Overrides¶
| Rule | Google Default | Our Standard |
|---|---|---|
| Line length | 100 | 120 characters |
| Import ordering | Google order | Configured in google-java-format |
| Wildcard imports | Prohibited | Prohibited ✅ |
| Braces | Required even for single-line | Required ✅ |
Key Rules¶
Immutability¶
- Prefer immutable value objects. Use Java
recordfor value types (Java 16+). - Mark fields
finalwherever possible. - Use unmodifiable collections:
List.of(),Map.of(),Collections.unmodifiableList().
Optionals¶
- Use
Optional<T>only as a method return type — never as a field or parameter. - Never call
.get()without.isPresent()check — use.orElse(),.orElseThrow(), or.ifPresent().
Modern Java Features (Required for Java 21)¶
- Use records for immutable data carriers.
- Use sealed classes for closed type hierarchies.
- Use pattern matching for instanceof — no explicit cast after
instanceof. - Use text blocks for multi-line strings.
- Use switch expressions instead of switch statements where possible.
// ✅ Modern Java 21
sealed interface Shape permits Circle, Rectangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double width, double height) implements Shape {}
double area(Shape shape) {
return switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.width() * r.height();
};
}
Exception Handling¶
- Never catch
ExceptionorThrowablewithout re-throwing or logging with full context. - Never swallow exceptions silently.
- Use checked exceptions only for recoverable conditions; use unchecked for programming errors.
- Create domain-specific exceptions (e.g.,
OrderNotFoundException extends RuntimeException).
Last reviewed: 2025-Q4 | Owner: Java Guild