GoogleNonConstantFieldName
Since Checkstyle 13.3.0
Description
Checks that non-constant field names conform to the
Google Java Style Guide for non-constant field naming.
This check enforces Google's specific non-constant field naming requirements:
- Non-constant field names must start with a lowercase letter and use uppercase letters for word boundaries.
- Underscores may be used to separate adjacent numbers (e.g., version
numbers like
guava33_4_5), but NOT between letters and digits.
Static fields are skipped because Checkstyle cannot determine type immutability
to distinguish constants from non-constants. Fields in interfaces and annotations
are also skipped because they are implicitly public static final (constants)
Examples
Example with violations:
<module name="Checker">
<module name="TreeWalker">
<module name="GoogleNonConstantFieldName"/>
</module>
</module>
Code Example:
class Example1 {
static final int STATIC_FINAL = 0;
private static final int Invalid_Name = 1;
static int staticField;
private static int Static_Bad;
interface ExampleInterface {
int CONSTANT = 1;
String Invalid_Name = "test";
}
@interface ExampleAnnotation {
int DEFAULT = 0;
String Bad_Name = "test";
}
final int Bad = 0;
// violation above, 'Non-constant field name 'Bad' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
public final int mValue = 0;
// violation above, 'Non-constant field name 'mValue' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
private final int f = 0;
// violation above, 'Non-constant field name 'f' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
protected final int foo_bar = 0;
// violation above, 'Non-constant field name 'foo_bar' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
int fA;
// violation above, 'Non-constant field name 'fA' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
public int mField;
// violation above, 'Non-constant field name 'mField' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
private int foo$bar;
// violation above, 'Non-constant field name 'foo\$bar' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
protected int a;
// violation above, 'Non-constant field name 'a' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
int gradle_9_5_1;
// violation above, 'Non-constant field name 'gradle_9_5_1' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
int _foo;
// violation above, 'Non-constant field name '_foo' must start lowercase, be at least 2 chars, avoid single lowercase letter followed by uppercase, contain only letters, digits or underscores, with underscores allowed only between adjacent digits.'
}
Example with valid names:
<module name="Checker">
<module name="TreeWalker">
<module name="GoogleNonConstantFieldName"/>
</module>
</module>
Code Example:
class Example2 {
static final int STATIC_FINAL = 0;
private static final int PRIVATE_STATIC_FINAL = 1;
static int staticField;
private static int privateStatic;
interface ExampleInterface {
int CONSTANT = 1;
String VALID_NAME = "test";
}
@interface ExampleAnnotation {
int DEFAULT = 0;
String GOOD_NAME = "test";
}
final int foo = 0;
public final int httpClient = 0;
private final int version123 = 0;
protected final int guava33_4_5 = 0;
int bar;
public int xmlParser;
private int count99;
protected int release22_1_0;
int id;
int version1_2;
}
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






