Checkstyle is a command-line tool that can be used to determine if your source code follows a set of style rules. It also checks for common programming mistakes, such as class and method design problems.
You can download the latest version as a JAR file from http://checkstyle.sourceforge.net/. To run Checkstyle, move (or copy) the JAR file to the same directory as your program. Open a terminal in that location, and run the following command:
java -jar checkstyle-*-all.jar -c /google_checks.xml *.java
The * characters are wildcards that match whatever version of Checkstyle you have and whatever Java source files are present. The output indicates the file and line number of each problem. This example refers to a method beginning on line 93, column 5 of
Hello.java:93:5: Missing a Javadoc comment
/google_checks.xml is inside the JAR file and represents most of Google’s style rules. You can alternatively use
/sun_checks.xml or provide your own configuration file. See Checkstyle’s website for more information.
If you apply Checkstyle to your source code often, you will likely internalize good style habits over time. But there are limits to what automatic style checkers can do. In particular, they can’t evaluate the quality of your comments, the meaning of your variable names, or the structure of your algorithms.
Good comments make it easier for experienced developers to identify errors in your code. Good variable names communicate the intent of your program and how the data is organized. And good programs are designed to be efficient and demonstrably correct.