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;
  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 myValue = 0;
  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 foo = 0;
  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 fooBar = 0;
  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 fieldA;
  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."""
  int xmlParser;
  int xml$parser;
  // violation above """Non-constant field name 'xml\$parser' 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 gradle9_5_1;
  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 foo2;
  int _foo2;
  // violation above """Non-constant field name '_foo2' 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 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.naming.GoogleNonConstantFieldNameCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker