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.

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;

  final int Bad = 0;
  // violation above, 'Non-constant field name 'Bad' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  public final int mValue = 0;
  // violation above, 'Non-constant field name 'mValue' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  private final int f = 0;
  // violation above, 'Non-constant field name 'f' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  protected final int foo_bar = 0;
  // violation above, 'Non-constant field name 'foo_bar' has invalid underscore usage, underscores are only allowed between adjacent digits.'

  int fA;
  // violation above, 'Non-constant field name 'fA' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  public int mField;
  // violation above, 'Non-constant field name 'mField' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  private int foo$bar;
  // violation above, 'Non-constant field name 'foo\$bar' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  protected int a;
  // violation above, 'Non-constant field name 'a' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  int gradle_9_5_1;
  // violation above, 'Non-constant field name 'gradle_9_5_1' has invalid underscore usage, underscores are only allowed between adjacent digits.'

  int foo1_bar;
  // violation above, 'Non-constant field name 'foo1_bar' has invalid underscore usage, underscores are only allowed between adjacent digits.'

  int _foo;
  // violation above, 'Non-constant field name '_foo' has invalid underscore usage, underscores are only allowed 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 Invalid_Name = 1;

  static int staticField;
  private static int Static_Bad;

  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 myVariableName;
  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

Parent Module

TreeWalker