Since Checkstyle 3.0
static
, non-final
variable names
conform to a specified pattern.
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-zA-Z0-9]*$" |
3.0 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="StaticVariableName"/> </module> </module>
Code Example:
class Example1 { public static int goodStatic = 2; private static int badStatic = 2; public static int ItStatic1 = 2; // violation, 'must match pattern' protected static int ItStatic2 = 2; // violation, 'must match pattern' private static int ItStatic = 2; // violation, 'must match pattern' public static int it_static = 2; // violation, 'must match pattern' public static int It_Static = 2; // violation, 'must match pattern' private static int It_Static1 = 2; // violation, 'must match pattern' static int It_Static2 = 2; // violation, 'must match pattern' }
An example of how to suppress the check to public and protected types is:
<module name="Checker"> <module name="TreeWalker"> <module name="StaticVariableName"> <property name="applyToPublic" value="false"/> <property name="applyToProtected" value="false"/> </module> </module> </module>
Code Example:
class Example2 { public static int goodStatic = 2; private static int badStatic = 2; public static int ItStatic1 = 2; protected static int ItStatic2 = 2; private static int ItStatic = 2; // violation, 'must match pattern' public static int it_static = 2; public static int It_Static = 2; private static int It_Static1 = 2; // violation, 'must match pattern' static int It_Static2 = 2; // violation, 'must match pattern' }
An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores. Also, suppress the check from being applied to private and package-private types:
<module name="Checker"> <module name="TreeWalker"> <module name="StaticVariableName"> <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/> <property name="applyToPrivate" value="false"/> <property name="applyToPackage" value="false"/> </module> </module> </module>
Code Example:
class Example3 { public static int goodStatic = 2; private static int badStatic = 2; public static int ItStatic1 = 2; // violation, 'must match pattern' protected static int ItStatic2 = 2; // violation, 'must match pattern' private static int ItStatic = 2; public static int it_static = 2; public static int It_Static = 2; // violation, 'must match pattern' private static int It_Static1 = 2; static int It_Static2 = 2; }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.naming