Class DeclarationOrderCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class DeclarationOrderCheck
    extends AbstractCheck
    Checks that the parts of a class or interface declaration appear in the order suggested by the Code Conventions for the Java Programming Language.
    1. Class (static) variables. First the public class variables, then the protected, then package level (no access modifier), and then the private.
    2. Instance variables. First the public class variables, then the protected, then package level (no access modifier), and then the private.
    3. Constructors
    4. Methods

    ATTENTION: the check skips class fields which have forward references from validation due to the fact that we have Checkstyle's limitations to clearly detect user intention of fields location and grouping. For example,

    
          public class A {
              private double x = 1.0;
              private double y = 2.0;
              public double slope = x / y; // will be skipped from validation due to forward reference
          }
     

    Available options:

    • ignoreModifiers
    • ignoreConstructors

    Purpose of ignore* option is to ignore related violations, however it still impacts on other class members.

    For example:

    
         class K {
             int a;
             void m(){}
             K(){}  <-- "Constructor definition in wrong order"
             int b; <-- "Instance variable definition in wrong order"
         }
     

    With ignoreConstructors option:

    
         class K {
             int a;
             void m(){}
             K(){}
             int b; <-- "Instance variable definition in wrong order"
         }
     

    With ignoreConstructors option and without a method definition in a source class:

    
         class K {
             int a;
             K(){}
             int b; <-- "Instance variable definition in wrong order"
         }
     

    An example of how to configure the check is:

     <module name="DeclarationOrder"/>
     
    Author:
    r_auckenthaler