ConstantName
Since Checkstyle 3.0
Description
Checks that constant names conform to a specified pattern.
A constant is a static and final field or an
interface/annotation field, except serialVersionUID and
serialPersistentFields.
Properties
name | description | type | default value | since |
---|---|---|---|---|
applyToPackage | Control if check should apply to package-private members. | boolean | true |
5.0 |
applyToPrivate | Control if check should apply to private members. | boolean | true |
5.0 |
applyToProtected | Control if check should apply to protected members. | boolean | true |
5.0 |
applyToPublic | Control if check should apply to public members. | boolean | true |
5.0 |
format | Sets the pattern to match valid identifiers. | Pattern | "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$" |
3.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="ConstantName"/>
</module>
</module>
Example:
class Example1 {
public final static int FIRST_CONSTANT1 = 10;
protected final static int SECOND_CONSTANT2 = 100;
final static int third_Constant3 = 1000; // violation 'must match pattern'
private final static int fourth_Const4 = 50; // violation 'must match pattern'
public final static int log = 10; // violation 'must match pattern'
protected final static int logger = 50; // violation 'must match pattern'
final static int loggerMYSELF = 5; // violation 'must match pattern'
final static int MYSELF = 100;
protected final static int myselfConstant = 1; // violation 'must match pattern'
}
The following configuration apart from names allowed by default allows log
or logger
:
<module name="Checker">
<module name="TreeWalker">
<module name="ConstantName">
<property name="format"
value="^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
</module>
</module>
</module>
Code Example:
class Example2 {
public final static int FIRST_CONSTANT1 = 10;
protected final static int SECOND_CONSTANT2 = 100;
final static int third_Constant3 = 1000; // violation 'must match pattern'
private final static int fourth_Const4 = 50; // violation 'must match pattern'
public final static int log = 10;
protected final static int logger = 50;
final static int loggerMYSELF = 5; // violation 'must match pattern'
final static int MYSELF = 100;
protected final static int myselfConstant = 1; // violation 'must match pattern'
}
The following configuration skip validation on public constant field and protected constant field.
<module name="Checker">
<module name="TreeWalker">
<module name="ConstantName">
<property name="applyToPublic" value="false"/>
<property name="applyToProtected" value="false"/>
</module>
</module>
</module>
Code Example:
class Example3 {
public final static int FIRST_CONSTANT1 = 10;
protected final static int SECOND_CONSTANT2 = 100;
final static int third_Constant3 = 1000; // violation 'must match pattern'
private final static int fourth_Const4 = 50; // violation 'must match pattern'
public final static int log = 10;
protected final static int logger = 50;
final static int loggerMYSELF = 5; // violation 'must match pattern'
final static int MYSELF = 100;
protected final static int myselfConstant = 1;
}
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.naming