DeclarationOrder

Since Checkstyle 3.2

Description

Checks that the parts of a class, record, or interface declaration appear in the order suggested by the Code Conventions for the Java Programming Language.

According to Code Conventions for the Java Programming Language , the parts of a class or interface declaration should appear in the following order:

  1. Class (static) variables. First the public class variables, then protected, then package level (no access modifier), and then private.
  2. Instance variables. First the public class variables, then protected, then package level (no access modifier), and then private.
  3. Constructors
  4. Methods

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

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
}
        

Properties

name description type default value since
ignoreConstructors Control whether to ignore constructors. boolean false 5.2
ignoreModifiers Control whether to ignore modifiers (fields, ...). boolean false 5.2

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="DeclarationOrder"/>
  </module>
</module>
        

Example:

public class Example1 {

  public int a;
  protected int b;
  public int c;            // violation, variable access definition in wrong order

  Example1() {
    this.a = 0;
  }

  public void foo() {
    // This method does nothing
  }

  Example1(int a) {            // violation, constructor definition in wrong order
    this.a = a;
  }

  private String name;     // violation, instance variable declaration in wrong order
}
        

To configure the check to ignore validation of constructors:

<module name="Checker">
  <module name="TreeWalker">
    <module name="DeclarationOrder">
      <property name="ignoreConstructors" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example2 {

  public int a;
  protected int b;
  public int c;            // violation, variable access definition in wrong order

  Example2() {
    this.a = 0;
  }

  public void foo() {
    // This method does nothing
  }

  Example2(int a) {            // OK, validation of constructors ignored
    this.a = a;
  }

  private String name;     // violation, instance variable declaration in wrong order
}
        

To configure the check to ignore modifiers:

<module name="Checker">
  <module name="TreeWalker">
    <module name="DeclarationOrder">
      <property name="ignoreModifiers" value="true"/>
    </module>
  </module>
</module>
        

Example:

public class Example3 {

  public int a;
  protected int b;
  public int c;            // OK, access modifiers not considered while validating

  Example3() {
    this.a = 0;
  }

  public void foo() {
    // This method does nothing
  }

  Example3(int a) {            // violation, constructor definition in wrong order
    this.a = a;
  }

  private String name;     // violation, instance variable declaration in wrong order
}
        

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.coding

Parent Module

TreeWalker