DeclarationOrder
Since Checkstyle 3.2
Description
According to Code Conventions for the Java Programming Language , the parts of a class or interface declaration should appear in the following order:
- Class (static) variables. First the public class variables, then protected, then package level (no access modifier), and then private.
- Instance variables. First the public class variables, then protected, then package level (no access modifier), and then private.
- Constructors
- 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
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
- declaration.order.access
- declaration.order.constructor
- declaration.order.instance
- declaration.order.static
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