ConstructorsDeclarationGrouping
Since Checkstyle 10.17.0
Description
Rationale: Grouping constructors together in a class improves code readability and maintainability. It allows developers to easily understand the different ways an object can be instantiated and the tasks performed by each constructor.
Properties
| name | description | type | default value | since |
|---|---|---|---|---|
| orderByIncreasingParameterCount | Control whether to enforce order by increasing parameter count (arity) or not. | boolean | false |
13.5.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="ConstructorsDeclarationGrouping"/>
</module>
</module>
Example:
public class Example1 {
int x;
Example1() {}
Example1(String s) {}
// comments between constructors are allowed.
Example1(int x, int y, int z) {}
int a = 0;
// violation 2 lines below """Constructors should be grouped together.
// The last grouped constructor is declared at line '19'."""
Example1(String s, int x) {}
private enum ExampleEnum {
ONE, TWO, THREE;
ExampleEnum() {}
void foo() {}
// violation 2 lines below """Constructors should be grouped together.
// The last grouped constructor is declared at line '31'."""
ExampleEnum(int x, int y) {}
// violation 2 lines below """Constructors should be grouped together.
// The last grouped constructor is declared at line '31'."""
ExampleEnum(String s) {}
}
class InputWithOrderedCtors {
InputWithOrderedCtors() {}
InputWithOrderedCtors(String s) {}
InputWithOrderedCtors(int x) {}
InputWithOrderedCtors(String s, int x) {}
}
}
To configure the check according to OpenJDK guidelines, where constructors should be grouped together and ordered by increasing parameter count:
<module name="Checker">
<module name="TreeWalker">
<module name="ConstructorsDeclarationGrouping">
<property name="orderByIncreasingParameterCount" value="true"/>
</module>
</module>
</module>
Example:
public class Example2 {
int x;
Example2() {}
Example2(String s) {}
// comments between constructors are allowed.
Example2(int x, int y, int z) {}
int a = 0;
// violation 2 lines below """Constructors should be grouped together.
// The last grouped constructor is declared at line '21'."""
Example2(String s, int x) {}
// violation above 'Constructors should be ordered by
// increasing parameter count.'
private enum ExampleEnum {
ONE, TWO, THREE;
ExampleEnum() {}
void foo() {}
// violation 2 lines below """Constructors should be grouped together.
// The last grouped constructor is declared at line '35'."""
ExampleEnum(int x, int y) {}
// violation 2 lines below """Constructors should be grouped together.
// The last grouped constructor is declared at line '35'."""
ExampleEnum(String s) {}
// violation above 'Constructors should be ordered by
// increasing parameter count.'
}
class InputWithOrderedCtors {
InputWithOrderedCtors() {}
InputWithOrderedCtors(String s) {}
InputWithOrderedCtors(int x) {}
InputWithOrderedCtors(String s, int x) {}
}
}
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.coding.ConstructorsDeclarationGroupingCheck
Use this fully qualified class name in configuration when an exact class reference is required.






