Skip to main content
Clean Code 50 XP · 5 min

Java-Specific Heuristics (J1–J3)

Three Java-focused heuristics: avoid wildcard imports, never inherit constants, and prefer enums over integer constants.

Showing
Ad (728×90)

J1 & J2: Imports and Constant Inheritance

J1: Wildcard imports (import java.util.*) hide exactly which classes a file depends on. Explicit imports are a precise, searchable record of what the class actually needs. J2: Implementing an interface solely to inherit its constants is an anti-pattern called the Constant Interface Pattern — it pollutes the implementing class's public API. Use static import instead: you get the constant without the inheritance coupling.

J3: Enums vs Integer Constants

public static final int SUNDAY = 0 gives zero type safety — any int can be passed where a day is expected. An enum DayOfWeek.SUNDAY is named, typed, and exhaustive. Switch statements on enums generate compiler warnings when a case is missing. Enums can carry methods and data. The same principle applies in any language: TypeScript enum and Python enum.Enum provide the same guarantees.

Code Challenge

Replace integer constants with a typed enum to gain exhaustiveness and type safety (J3).

Key takeaway

J3 is the most universally applicable: replacing int constants with typed enums eliminates an entire class of silent bugs and makes switch/match statements exhaustively verifiable.

Done with this lesson?

Mark it complete to earn XP and track your progress.