ModifierOrder
Since Checkstyle 3.0
Description
Checks that the order of modifiers conforms to the suggestions in
the Java
Language specification, § 8.1.1, 8.3.1, 8.4.3 and
9.4. The correct order is:
-
public
-
protected
-
private
-
abstract
-
default
-
static
-
sealed
-
non-sealed
-
final
-
transient
-
volatile
-
synchronized
-
native
-
strictfp
In additional, modifiers are checked to ensure all annotations are declared before all other modifiers.
Rationale: Code is easier to read if everybody follows a standard.
ATTENTION: We skip type annotations from validation.
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="ModifierOrder"/>
</module>
</module>
Code:
public class Example1 {
public static final int MAX_VALUE = 100;
// violation below "'private' modifier out of order with the JLS suggestions"
final private String example = "Example";
//violation below, 'annotation modifier does not precede non-annotation modifiers'
public @Deprecated class Example {}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.modifier