Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is the difference between SpotBugs, PMD and Checkstyle? Which would catch a possible NullPointerException, an unused private field, and wrong indentation?

The three tools look at different things. SpotBugs analyses compiled bytecode for bug patterns such as null dereferences and broken equals implementations; PMD analyses source code for problems such as unused variables, empty catch blocks and excessive complexity; and Checkstyle checks coding style and formatting conventions such as indentation and naming. So SpotBugs catches the possible NullPointerException, PMD the unused private field, and Checkstyle the wrong indentation, although many teams now replace Checkstyle's formatting rules with an automatic formatter such as Spotless with google-java-format.

Why this question is asked

These three tools have been part of Java builds for a long time, and many projects run all of them, often without anyone remembering why each one is there. Interviewers ask this question because knowing what each tool inspects explains what it can and cannot find, and that is what lets us choose sensibly instead of piling up overlapping checks. In this answer, we will see what static analysis means, look at each tool and what it reads, apply them to one small class with all three problems, and then see where an automatic formatter fits.

1A Quick Recap: Static Analysis

Static analysis means examining code without running it. A tool reads either the source files or the compiled class files, looks for patterns known to cause problems, and reports them with a rule name and a location. It complements tests: tests check that the code does what we expect on the inputs we thought of, while static analysis finds classes of mistakes on paths we never tested.

The key difference between SpotBugs, PMD and Checkstyle is what each one reads and what kind of problem it is designed to find. Once we know that, the answer to which tool catches what follows naturally.

What each tool inspects
ToolReadsLooks for
SpotBugsCompiled bytecode (.class files)Likely bugs: null dereferences, bad equals
PMDSource code (syntax tree)Bad practices: unused code, empty catch, complexity
CheckstyleSource code (tokens and layout)Style: indentation, naming, braces, Javadoc

2SpotBugs: Bug Patterns in Bytecode

SpotBugs, the successor of FindBugs, analyses the compiled classes. Because it works on bytecode, it sees the code after the compiler has resolved types and method calls, and it can follow data flow inside a method: where a value comes from, whether it can be null on some path, and where it is used. Its detectors find bug patterns such as a possible null pointer dereference on some path, equals overridden without hashCode, comparing strings with ==, ignoring the return value of a method that has no side effects, and inconsistent synchronization.

Since it needs compiled code, it runs after compilation, for example in Maven's verify phase. Its plugin FindSecBugs adds security patterns such as SQL injection and weak cryptography.

3PMD: Problems in the Source Code

PMD parses the source code into a syntax tree, a structured representation of classes, methods and statements, and runs rules over it. Its rules cover unused code (private fields, local variables, parameters and private methods), empty catch blocks that swallow exceptions, overly complex methods, and many best practices and design rules. Rules can be written as XPath queries over the syntax tree, which makes custom rules easy to add.

PMD also includes CPD, the copy-paste detector, which finds duplicated blocks of code. Because PMD reads source rather than bytecode, it sees things the compiler throws away, such as the exact structure of the code, but its data flow analysis is more limited than SpotBugs' for bugs like null dereferences.

4Checkstyle: Style and Formatting Conventions

Checkstyle checks that the source follows a coding standard, such as the Google Java Style or Sun conventions, or a company's own. Its modules check indentation, line length, brace placement, whitespace, naming patterns for classes, methods and constants, import order and Javadoc presence. It does not try to find bugs; a file can pass Checkstyle perfectly and still throw a NullPointerException on every call.

Its value is consistency: code that looks the same everywhere is easier to read and review, and reviewers do not waste comments on layout.

5One Class, Three Problems, Three Tools

Here is a small class with all three problems from the question, and what each tool reports. The exact message text varies between versions, so check yours.

Java
public class CustomerService {
private final Clock clock = Clock.systemUTC(); // never used
public String displayName(Customer c) {
String nick = c.nickname(); // may return null
if (nick.isEmpty()) { // dereferenced without a check
return c.fullName(); // indented 6 spaces instead of 4
}
return nick;
}
}
// SpotBugs (if it knows nickname() may return null, e.g. via @Nullable):
// NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE Possible null pointer dereference of nick
// PMD:
// UnusedPrivateField Avoid unused private fields such as 'clock'
// Checkstyle (Google style):
// Indentation 'return' has incorrect indentation level 14, expected level should be 12

Notice why each tool is the right one. The null problem needs data flow: knowing that nickname() can return null, SpotBugs follows the value to its use and flags the path where it is dereferenced. Neither PMD nor Checkstyle is built for that. The unused field is visible in the source structure, which is exactly what PMD's rules inspect; SpotBugs has a similar detector, but PMD is the usual tool for unused code. The indentation is pure layout, which only Checkstyle cares about.

There is a subtlety in the SpotBugs case: it needs to know that nickname() can return null. It infers this well for values it can see, such as an explicit null assignment, and for return values marked with a @Nullable annotation it recognises. Without such information it may stay silent, which is one reason tools such as NullAway, which enforce annotations at compile time, have become popular.

6Where an Automatic Formatter Fits

Checking indentation and then asking a person to fix it by hand is slow. An automatic formatter fixes it instead. Spotless is a build plugin for Maven and Gradle that applies a formatter, such as google-java-format or palantir-java-format, to every source file, and can fail the build if files are not formatted. Developers run one command, or let the IDE format on save, and formatting problems disappear entirely.

Java
// build.gradle.kts (check current plugin versions)
plugins { id("com.diffplug.spotless") version "7.0.4" }
spotless {
java {
googleJavaFormat()
removeUnusedImports()
}
}
// ./gradlew spotlessCheck -> fails: CustomerService.java is not formatted
// ./gradlew spotlessApply -> rewrites the file with correct indentation
// ./gradlew spotlessCheck -> passes

That is why many teams keep Checkstyle only for rules a formatter cannot fix, such as naming conventions or banned imports, and drop its formatting rules. A common modern set is a formatter for layout, PMD or Error Prone for code-level problems, and SpotBugs or the compiler-based NullAway for null safety.

7Follow-up questions

Do these tools overlap?

Yes, somewhat. PMD and SpotBugs both have rules for unused code and some bad practices, and SonarQube's Java analyzer covers much of what all three do. Running every tool with every rule creates duplicate findings for the same line. It is better to give each tool a clear job, for example SpotBugs for bugs, PMD for code-level practices and a formatter for layout, and disable overlapping rules.

Why does SpotBugs need compiled classes while PMD does not?

SpotBugs' detectors are built on bytecode analysis, which gives it resolved types, method calls and control flow in a form that is convenient for data flow analysis. PMD's rules are written against the source syntax tree, so it can run on source alone, before or without compilation, and see things the compiler discards. That is also why SpotBugs runs later in the build.

Which of these would catch a resource leak, such as an unclosed stream?

SpotBugs has detectors for streams and database resources that may not be closed on all paths, and PMD has a CloseResource rule that checks source patterns. Both can catch common cases. The best prevention is try-with-resources, which makes the leak impossible, and the tools help find places that do not use it.

8Conclusion

In this answer, we saw that SpotBugs reads bytecode and finds likely bugs through data flow, such as a possible null dereference; PMD reads the source syntax tree and finds bad practices such as an unused private field; and Checkstyle checks style conventions such as indentation. Each catches one of the three problems in our example, and for formatting in particular an automatic formatter such as Spotless with google-java-format replaces checking with fixing.

What to say in the interviewAbout 70 seconds aloud

Open with

The difference is what each tool reads and what it looks for. SpotBugs analyses compiled bytecode and looks for bug patterns using data flow, things like a possible null pointer dereference on some path, equals without hashCode, or comparing strings with ==.

Then, if they want more

PMD parses the source code into a syntax tree and looks for bad practices and design problems, like unused private fields and variables, empty catch blocks and overly complex methods, and it includes a copy-paste detector. Checkstyle checks coding style and formatting against a standard, like indentation, naming, braces and Javadoc; it does not look for bugs at all.

So for the three examples: the possible NullPointerException is SpotBugs, because it needs to follow where a value can be null, and it works best when the code uses nullable annotations; the unused private field is PMD; and the wrong indentation is Checkstyle. In practice, I would not have people fix indentation by hand.

I would use Spotless with google-java-format so the build formats the code automatically, keep Checkstyle only for rules a formatter cannot fix, like naming, and give SpotBugs and PMD clear, non-overlapping jobs.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between SpotBugs, PMD and Checkstyle? Which would catch a...": https://scrivio-interview-library.netlify.app/java/code-quality-tools/da23e774e2/