ModifierOrder

Since Checkstyle 3.0

Description

Validates that the modifiers appear in the correct, standard order. By default 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 default order is:
  1. public
  2. protected
  3. private
  4. abstract
  5. default
  6. static
  7. sealed
  8. non-sealed
  9. final
  10. transient
  11. volatile
  12. synchronized
  13. native
  14. strictfp

Additionally, 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.

Properties

name description type default value since
modifiersOrder Set the order of modifiers. String[] abstract, default, final, native, non-sealed, private, protected, public, sealed, static, strictfp, synchronized, transient, volatile 14.2.0

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 "'public' modifier out of order with the JLS suggestions"
  final public String exampleOne = "ExampleOne";
  // violation below "'public' modifier out of order with the JLS suggestions"
  static public int exampleTwo;

  private static void method() {}

  // violation below 'annotation modifier does not precede non-annotation modifiers'
  public @Deprecated class Example {}

  sealed strictfp interface Test permits TestClass {}

  final class TestClass implements Test {}

}

To configure the check for openjdk style:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ModifierOrder">
      <property name="modifiersOrder" value="public, private,
                protected, abstract, static, final, transient, volatile,
                default, synchronized, native, strictfp"/>
    </module>
  </module>
</module>

Code:


public class Example2 {
  public static final int MAX_VALUE = 100;

  // violation below 'public' modifier out of order with the defined modifier order.
  final public String exampleOne = "ExampleOne";
  // violation below 'public' modifier out of order with the defined modifier order.
  static public int exampleTwo;

  private static void method() {}

  // violation below 'annotation modifier does not precede non-annotation modifiers'
  public @Deprecated class Example {}

  sealed strictfp interface Test permits TestClass {}
  // violation above 'strictfp' modifier out of order.
  final class TestClass implements Test {}

}

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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker