GoogleNonConstantFieldName

Since Checkstyle 13.2.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

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="GoogleNonConstantFieldName"/>
  </module>
</module>

Code Example:


class Example1 {

  int fooBar;
  int guava33_4_5;
  int httpClient;
  int version1_2;

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

  int f;
  // 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.'

  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 foo_bar;
  // violation above, 'Non-constant field name 'foo_bar' has invalid underscore usage, underscores are only allowed between adjacent digits.'

  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.'

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

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