MemberName

Since Checkstyle 3.0

Description

Checks that instance variable names conform to a specified pattern.

Properties

name description type default value since
applyToPackage Control if check should apply to package-private members. boolean true 3.4
applyToPrivate Control if check should apply to private members. boolean true 3.4
applyToProtected Control if check should apply to protected members. boolean true 3.4
applyToPublic Control if check should apply to public members. boolean true 3.4
format Sets the pattern to match valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0

Examples

To configure the check:

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

Code Example:

class Example1 {
  public int num1;
  protected int num2;
  final int num3 = 3;
  private int num4;

  static int num5;
  public static final int CONSTANT = 1;

  public int NUM1; // violation

  protected int NUM2; // violation

  int NUM3; // violation

  private int NUM4; // violation

}
        

An example of how to configure the check for names that begin with "m", followed by an upper case letter, and then letters and digits. Also, suppress the check from being applied to protected and package-private member:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MemberName">
      <property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/>
      <property name="applyToProtected" value="false"/>
      <property name="applyToPackage" value="false"/>
    </module>
  </module>
</module>
        

Code Example:

class Example2 {
  public int num1; // violation

  protected int num2;
  int num3;
  private int num4; // violation

}
        

An example of how to suppress the check which is applied to public and private member:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MemberName">
      <property name="applyToPublic" value="false"/>
      <property name="applyToPrivate" value="false"/>
    </module>
  </module>
</module>
        

Code Example:

class Example3 {
  public int NUM1;
  protected int NUM2; // violation

  int NUM3; // violation

  private int NUM4;
}
        

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