Java / Java 21 Coding Standards Interview Questions
How can you optimize static analysis coverage for Java 21 features like pattern matching?
Older static analysis rule sets were written before pattern matching, sealed types, and records existed, so out-of-the-box configurations often miss issues specific to them - an unguarded pattern that silently shadows a broader case, or a record whose compact constructor skips validation that the standard requires.
// custom rule idea: flag a record with mutable field types like ArrayList public record Cart(List<Item> items) {} // should require an unmodifiable copy in the compact constructor
Optimizing coverage means updating the toolchain deliberately: upgrading Checkstyle, PMD, and SpotBugs (or Error Prone) to versions that understand Java 21 syntax, then explicitly enabling their newer rule categories for switch exhaustiveness, redundant instanceof-then-cast patterns, and record field mutability, since many of these rules ship disabled by default even in updated tool versions.
The standard practice is to also add project-specific custom rules for domain patterns the generic tools cannot know about - for example, requiring every record with a collection component to defensively copy it in a compact constructor - and to track rule coverage over time so newly adopted Java 21 idioms do not silently outrun what the analyzer actually checks.
More Related questions...