ModifierOrder

Description

Checks that the order of modifiers conforms to the suggestions in the Java Language specification, sections 8.1.1, 8.3.1 and 8.4.3. The correct order is:

  1. public
  2. protected
  3. private
  4. abstract
  5. static
  6. final
  7. transient
  8. volatile
  9. synchronized
  10. native
  11. strictfp

ATTENTION: We skip type annotations from validation.

Examples

To configure the check:

<module name="ModifierOrder"/>
        

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.modifier

Parent Module

TreeWalker

RedundantModifier

Description

Checks for redundant modifiers in:

  1. Interface and annotation definitions.
  2. Final modifier on methods of final and anonymous classes.
  3. Inner interface declarations that are declared as static.
  4. Class constructors.
  5. Nested enum definitions that are declared as static.

Rationale: The Java Language Specification strongly discourages the usage of public and abstract for method declarations in interface definitions as a matter of style.

Interfaces by definition are abstract so the abstract modifier on the interface is redundant.

Classes inside of interfaces by definition are public and static, so the public and static modifiers on the inner classes are redundant. On the other hand, classes inside of interfaces can be abstract or non abstract. So, abstract modifier is allowed.

Fields in interfaces and annotations are automatically public, static and final, so these modifiers are redundant as well.

As annotations are a form of interface, their fields are also automatically public, static and final just as their annotation fields are automatically public and abstract.

Enums by definition are static implicit subclasses of java.lang.Enum<E>. So, the static modifier on the enums is redundant. In addition, if enum is inside of interface, public modifier is also redundant.

Nested enum types are always static by default.

Final classes by definition cannot be extended so the final modifier on the method of a final class is redundant.

Public modifier for constructors in non-public non-protected classes is always obsolete:

          public class PublicClass {
            public PublicClass() {} // OK
          }

          class PackagePrivateClass {
            public PackagePrivateClass() {} // violation expected
          }
        

There is no violation in the following example, because removing public modifier from ProtectedInnerClass constructor will make this code not compiling:

          package a;
          public class ClassExample {
            protected class ProtectedInnerClass {
              public ProtectedInnerClass () {}
            }
          }

          package b;
          import a.ClassExample;
          public class ClassExtending extends ClassExample {
            ProtectedInnerClass pc = new ProtectedInnerClass();
          }
        

Properties

name description type default value
tokens tokens to check subset of tokens METHOD_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, INTERFACE_DEF, CTOR_DEF, CLASS_DEF, ENUM_DEF. METHOD_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, INTERFACE_DEF, CTOR_DEF, CLASS_DEF, ENUM_DEF.

Examples

To configure the check:

<module name="RedundantModifier"/>
        

To configure the check to check only methods and not variables:

<module name="RedundantModifier">
  <property name="tokens" value="METHOD_DEF"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.modifier

Parent Module

TreeWalker