Content

Overview

Each of these naming modules validates identifiers for particular code elements. Valid identifiers for a naming module are specified by its format property. The value of format is a regular expression for valid identifiers.

AbbreviationAsWordInName

Since Checkstyle 5.8

Description

Validates abbreviations (consecutive capital letters) length in identifier name, it also allows to enforce camel case naming. Please read more at Google Style Guide to get to know how to avoid long abbreviations in names.

allowedAbbreviationLength specifies how many consecutive capital letters are allowed in the identifier. A value of 3 indicates that up to 4 consecutive capital letters are allowed, one after the other, before a violation is printed. The identifier 'MyTEST' would be allowed, but 'MyTESTS' would not be. A value of 0 indicates that only 1 consecutive capital letter is allowed. This is what should be used to enforce strict camel casing. The identifier 'MyTest' would be allowed, but 'MyTEst' would not be.

ignoreFinal, ignoreStatic, and ignoreStaticFinal control whether variables with the respective modifiers are to be ignored. Note that a variable that is both static and final will always be considered under ignoreStaticFinal only, regardless of the values of ignoreFinal and ignoreStatic. So for example if ignoreStatic is true but ignoreStaticFinal is false, then static final variables will not be ignored.

Properties

name description type default value since
allowedAbbreviationLength Indicate the number of consecutive capital letters allowed in targeted identifiers (abbreviations in the classes, interfaces, variables and methods names, ... ). int 3 5.8
allowedAbbreviations Specify list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma. String[] {} 5.8
ignoreFinal Allow to skip variables with final modifier. boolean true 5.8
ignoreStatic Allow to skip variables with static modifier. boolean true 5.8
ignoreStaticFinal Allow to skip variables with both static and final modifiers. boolean true 8.32
ignoreOverriddenMethods Allow to ignore methods tagged with @Override annotation (that usually mean inherited name). boolean true 5.8
tokens tokens to check subset of tokens CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , ENUM_CONSTANT_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF . CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , PATTERN_VARIABLE_DEF , RECORD_DEF , RECORD_COMPONENT_DEF . 5.8

Examples

To configure the check:

<module name="AbbreviationAsWordInName"/>
        

Example:

public class MyClass extends SuperClass { // OK, camel case
  int CURRENT_COUNTER; // violation, at most 4 consecutive capital letters allowed
  static int GLOBAL_COUNTER; // OK, static is ignored
  final Set<String> stringsFOUND = new HashSet<>(); // OK, final is ignored

  @Override
  void printCOUNTER() { // OK, overridden method is ignored
    System.out.println(CURRENT_COUNTER); // OK, only definitions are checked
  }

  void incrementCOUNTER() { // violation, at most 4 consecutive capital letters allowed
    CURRENT_COUNTER++; // OK, only definitions are checked
  }

  static void incrementGLOBAL() { // violation, static method is not ignored
    GLOBAL_COUNTER++; // OK, only definitions are checked
  }

}
        

To configure to include static variables and methods tagged with @Override annotation.

Configuration:

<module name="AbbreviationAsWordInName">
  <property name="ignoreStatic" value="false"/>
  <property name="ignoreOverriddenMethods" value="false"/>
</module>
        

Example:

public class MyClass extends SuperClass { // OK, camel case
  int CURRENT_COUNTER; // violation, at most 4 consecutive capital letters allowed
  static int GLOBAL_COUNTER; // violation, static is not ignored
  final Set<String> stringsFOUND = new HashSet<>(); // OK, final is ignored

  @Override
  void printCOUNTER() { // violation, overridden method is not ignored
    System.out.println(CURRENT_COUNTER); // OK, only definitions are checked
  }

  void incrementCOUNTER() { // violation, at most 4 consecutive capital letters allowed
    CURRENT_COUNTER++; // OK, only definitions are checked
  }

  static void incrementGLOBAL() { // violation, at most 4 consecutive capital letters allowed
    GLOBAL_COUNTER++; // OK, only definitions are checked
  }

}
        

To configure to check all variables and identifiers (including ones with the static modifier) and enforce no abbreviations (essentially camel case) except for words like 'XML' and 'URL'.

Configuration:

<module name="AbbreviationAsWordInName">
  <property name="tokens" value="VARIABLE_DEF,CLASS_DEF"/>
  <property name="ignoreStatic" value="false"/>
  <property name="allowedAbbreviationLength" value="0"/>
  <property name="allowedAbbreviations" value="XML,URL"/>
</module>
        

Example:

public class MyClass { // OK
  int firstNum; // OK
  int secondNUM; // violation, it allowed only 1 consecutive capital letter
  static int thirdNum; // OK, the static modifier would be checked
  static int fourthNUm; // violation, the static modifier would be checked,
                        // and only 1 consecutive capital letter is allowed
  String firstXML; // OK, XML abbreviation is allowed
  String firstURL; // OK, URL abbreviation is allowed
  final int TOTAL = 5; // OK, final is ignored
  static final int LIMIT = 10; // OK, static final is ignored
}
        

To configure to check variables, excluding fields with the static modifier, and allow abbreviations up to 2 consecutive capital letters ignoring the longer word 'CSV'.

Configuration:

<module name="AbbreviationAsWordInName">
  <property name="tokens" value="VARIABLE_DEF"/>
  <property name="ignoreStatic" value="true"/>
  <property name="allowedAbbreviationLength" value="1"/>
  <property name="allowedAbbreviations" value="CSV"/>
</module>
        

Example:

public class MyClass { // OK, ignore checking the class name
  int firstNum; // OK, abbreviation "N" is of allowed length 1
  int secondNUm; // OK
  int secondMYNum; // violation, found "MYN" but only
                   // 2 consecutive capital letters are allowed
  int thirdNUM; // violation, found "NUM" but it is allowed
                // only 2 consecutive capital letters
  static int fourthNUM; // OK, variables with static modifier
                        // would be ignored
  String firstCSV; // OK, CSV abbreviation is allowed
  String firstXML; // violation, XML abbreviation is not allowed
  final int TOTAL = 5; // OK, final is ignored
  static final int LIMIT = 10; // OK, static final is ignored
}
        

To configure to check variables, enforcing no abbreviations except for variables that are both static and final.

Configuration:

<module name="AbbreviationAsWordInName">
    <property name="tokens" value="VARIABLE_DEF"/>
    <property name="ignoreFinal" value="false"/>
    <property name="ignoreStatic" value="false"/>
    <property name="ignoreStaticFinal" value="true"/>
    <property name="allowedAbbreviationLength" value="0"/>
</module>
        

Example:

public class MyClass {
    public int counterXYZ = 1;                // violation
    public final int customerID = 2;          // violation
    public static int nextID = 3;             // violation
    public static final int MAX_ALLOWED = 4;  // OK, ignored
}
        

To configure to check variables, enforcing no abbreviations and ignoring static (but non-final) variables only.

Configuration:

<module name="AbbreviationAsWordInName">
    <property name="tokens" value="VARIABLE_DEF"/>
    <property name="ignoreFinal" value="false"/>
    <property name="ignoreStatic" value="true"/>
    <property name="ignoreStaticFinal" value="false"/>
    <property name="allowedAbbreviationLength" value="0"/>
</module>
        

Example:

public class MyClass {
    public int counterXYZ = 1;                // violation
    public final int customerID = 2;          // violation
    public static int nextID = 3;             // OK, ignored
    public static final int MAX_ALLOWED = 4;  // violation
}
        

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

AbstractClassName

Since Checkstyle 3.2

Description

Ensures that the names of abstract classes conforming to some regular expression and check that abstract modifier exists.

Rationale: Abstract classes are convenience base class implementations of interfaces, not types as such. As such they should be named to indicate this. Also if names of classes starts with 'Abstract' it's very convenient that they will have abstract modifier.

Properties

name description type default value since
format Specify valid identifiers. Pattern "^Abstract.+$" 3.2
ignoreModifier Control whether to ignore checking for the abstract modifier on classes that match the name. boolean false 5.3
ignoreName Control whether to ignore checking the name. Realistically only useful if using the check to identify that match name and do not have the abstract modifier. boolean false 5.3

Examples

To configure the check:

<module name="AbstractClassName"/>
        

Example:

abstract class AbstractFirstClass {} // OK
abstract class SecondClass {} // violation, it should match pattern "^Abstract.+$"
class AbstractThirdClass {} // violation, must be declared 'abstract'
class FourthClass {} // OK
        

To configure the check so that it check name but ignore abstract modifier:

<module name="AbstractClassName">
  <property name="ignoreModifier" value="true"/>
</module>
        

Example:

abstract class AbstractFirstClass {} // OK
abstract class SecondClass {} // violation, it should match pattern "^Abstract.+$"
class AbstractThirdClass {} // OK, no "abstract" modifier
class FourthClass {} // OK
        

To configure the check to ignore name validation when class declared as 'abstract'

<module name="AbstractClassName">
  <property name="ignoreName" value="true"/>
</module>
        

Example:

abstract class AbstractFirstClass {} // OK
abstract class SecondClass {} // OK, name validation is ignored
class AbstractThirdClass {} // violation, must be declared as 'abstract'
class FourthClass {} // OK, no "abstract" modifier
        

To configure the check with format:

<module name="AbstractClassName">
  <property name="format" value="^Generator.+$"/>
</module>
        

Example:

abstract class GeneratorFirstClass {} // OK
abstract class SecondClass {} // violation, must match pattern '^Generator.+$'
class GeneratorThirdClass {} // violation, must be declared 'abstract'
class FourthClass {} // OK, no "abstract" modifier
        

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

CatchParameterName

Since Checkstyle 6.14

Description

Checks that catch parameter names conform to a specified pattern.

Default pattern has the following characteristic:

  • allows names beginning with two lowercase letters followed by at least one uppercase or lowercase letter
  • allows e abbreviation (suitable for exceptions end errors)
  • allows ex abbreviation (suitable for exceptions)
  • allows t abbreviation (suitable for throwables)
  • prohibits numbered abbreviations like e1 or t2
  • prohibits one letter prefixes like pException
  • prohibits two letter abbreviations like ie or ee
  • prohibits any other characters than letters

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^(e|t|ex|[a-z][a-z][a-zA-Z]+)$" 6.14

Examples

To configure the check:

<module name="CatchParameterName"/>
        

Example:

public class MyTest {
  public void myTest() {
    try {
      // ...
    } catch (ArithmeticException e) { // OK
      // ...
    } catch (ArrayIndexOutOfBoundsException ex) { // OK
      // ...
    } catch (Throwable t) { // OK
      // ...
    } catch (IndexOutOfBoundsException e123) { // violation, digits
                                               // not allowed
      // ...
    } catch (NullPointerException ab) { // violation, should have at least
                                        // three characters if not e|t|ex
      // ...
    } catch (ArrayStoreException abc) { // OK
      // ...
    } catch (InterruptedException aBC) { // violation, first two characters
                                         // should be in lowercase
      // ...
    } catch (RuntimeException abC) { // OK
      // ...
    } catch (Exception abCD) { // OK
      // ...
    }
  }
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by any letters or digits is:

Configuration:

<module name="CatchParameterName">
  <property name="format" value="^[a-z][a-zA-Z0-9]+$"/>
</module>
        

Example:

public class MyTest {
  public void myTest() {
    try {
      // ...
    } catch (ArithmeticException ex) { // OK
      // ...
    } catch (ArrayIndexOutOfBoundsException ex2) { // OK
      // ...
    } catch (IOException thirdException) { // OK
      // ...
    } catch (Exception FourthException) { // violation, the initial letter
                                          // should be lowercase
      // ...
    }
  }
}
        

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

ClassTypeParameterName

Since Checkstyle 5.0

Description

Checks that class type parameter names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[A-Z]$" 5.0

Examples

To configure the check:

<module name="ClassTypeParameterName"/>
        

Example:

class MyClass1<T> {}        // OK
class MyClass2<t> {}        // violation
class MyClass3<abc> {}      // violation
class MyClass4<LISTENER> {} // violation
class MyClass5<RequestT> {} // violation
        

To configure the check for names that are uppercase word:

<module name="ClassTypeParameterName">
  <property name="format" value="^[A-Z]{2,}$"/>
</module>
        

Example:

class MyClass1<T> {}        // violation
class MyClass2<t> {}        // violation
class MyClass3<abc> {}      // violation
class MyClass4<LISTENER> {} // OK
class MyClass5<RequestT> {} // violation
        

To configure the check for names that are camel case word with T as suffix (Google Style):

<module name="ClassTypeParameterName">
  <property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
</module>
        

Example:

class MyClass1<T> {}        // violation
class MyClass2<t> {}        // violation
class MyClass3<abc> {}      // violation
class MyClass4<LISTENER> {} // violation
class MyClass5<RequestT> {} // OK
        

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

ConstantName

Since Checkstyle 3.0

Description

Checks that constant names conform to a specified pattern. A constant is a static and final field or an interface/annotation field, except serialVersionUID and serialPersistentFields.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$" 3.0
applyToPublic Controls whether to apply the check to public member. boolean true 5.0
applyToProtected Controls whether to apply the check to protected member. boolean true 5.0
applyToPackage Controls whether to apply the check to package-private member. boolean true 5.0
applyToPrivate Controls whether to apply the check to private member. boolean true 5.0

Examples

To configure the check:

<module name="ConstantName"/>
        

Example:

class MyClass {
  public final static int FIRST_CONSTANT1 = 10; // OK
  protected final static int SECOND_CONSTANT2 = 100; // OK
  final static int third_Constant3 = 1000; // violation, name 'third_Constant3' must
                                          // match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
  private final static int fourth_Const4 = 50; // violation, name 'fourth_Const4' must match
                                                // pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
}
        

The following configuration apart from names allowed by default allows log or logger:

<module name="ConstantName">
  <property name="format"
    value="^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$"/>
</module>
        

Code Example:

class MyClass {
  final static int log = 10; // OK
  final static int logger = 50; // OK
  final static int logMYSELF = 10; // violation, name 'logMYSELF' must match
                                   // pattern '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
  final static int loggerMYSELF = 5; // violation, name 'loggerMYSELF' must match
                                     // pattern '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
  final static int MYSELF = 100; // OK
  final static int myselfConstant = 1; // violation, name 'myselfConstant' must match pattern
                                       // '^log(ger)?$|^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
}
        

The following configuration skip validation on public constant field and protected constant field.

<module name="ConstantName">
  <property name="applyToPublic" value="false"/>
  <property name="applyToProtected" value="false"/>
</module>
        

Code Example:

class MyClass {
  public final static int firstConstant = 10; // OK
  protected final static int secondConstant = 100; // OK
  final static int thirdConstant = 1000; // violation, name 'thirdConstant' must
                                         // match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
  private final static int fourthConstant = 50; // violation, name 'fourthConstant' must match
                                                // pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'
}
        

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

IllegalIdentifierName

Since Checkstyle 8.36

Description

Checks identifiers with a regular expression for a set of illegal names, such as those that are restricted or contextual keywords. Examples include "yield", "record", "_", and "var". Please read more at Java Language Specification to get to know more about restricted keywords. Since this check uses a regular expression to specify valid identifiers, users can also prohibit the usage of certain symbols, such as "$", or any non-ascii character.

Properties

Examples

To configure the check:

Configuration:

<module name="IllegalIdentifierName"/>
        

Example:

public class TestClass {
    public static void main(String... args) {
        var var = 4; // violation, "var" should not be used as an identifier.
        int record = 15; // violation, "record" should not be used as an identifier.
        String yield = "yield"; // violation, "yield" should not be used as an identifier.

        record Record // violation, "Record" should not be used as an identifier.
        (Record record) { // violation, "record" should not be used as an identifier.
        }

        String yieldString = "yieldString"; // ok, part of another word
        record MyRecord(){} // ok, part of another word
        var variable = 2; // ok, part of another word
        String _; // violation, underscore should not be used as an identifier.
    }
}
        

To configure the check to include "open" and "transitive" in the set of illegal identifiers:

Configuration:

<module name="IllegalIdentifierName">
    <property name="format" value="(?i)^(?!(record|yield|var
                    |permits|sealed|open|transitive|_)$).+$"/>
</module>
        

Example:

public class TestClass {
    public static void main(String... args) {

    int open = 4; // violation, "open" should not be used as an identifier
    Object transitive = "transitive"; // violation, "transitive" should not
                                      // be used as an identifier

    int openInt = 4; // ok, "open" is part of another word
    Object transitiveObject = "transitiveObject"; // ok, "transitive" is part of another word
    }
}
        

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

InterfaceTypeParameterName

Since Checkstyle 5.8

Description

Checks that interface type parameter names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[A-Z]$" 5.8

Examples

To configure the check:

<module name="InterfaceTypeParameterName"/>
        

Code Example:

interface FirstInterface<T> {} // OK
interface SecondInterface<t> {} // violation, name 't' must match pattern '^[A-Z]$'
        

An example of how to configure the check for names that are only a single letter is:

<module name="InterfaceTypeParameterName">
   <property name="format" value="^[a-zA-Z]$"/>
</module>
        

Code Example:

interface FirstInterface<T> {} // OK
interface SecondInterface<t> {} // OK
interface ThirdInterface<type> {} // violation, name 'type' must
                                        // match pattern '^[a-zA-Z]$'
        

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

LambdaParameterName

Since Checkstyle 8.11

Description

Checks lambda parameter names.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 8.11

Examples

To configure the check:

<module name="LambdaParameterName"/>
        

Code Example:

Function<String, String> function1 = s -> s.toLowerCase(); // OK
Function<String, String> function2 = S -> S.toLowerCase(); // violation, name 'S'
                                               // must match pattern '^[a-z][a-zA-Z0-9]*$'
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters is:

<module name="LambdaParameterName">
  <property name="format" value="^[a-z]([a-zA-Z]+)*$"/>
</module>
        

Code Example:

class MyClass {
  Function<String, String> function1 = str -> str.toUpperCase().trim(); // OK
  Function<String, String> function2 = _s -> _s.trim(); // violation, name '_s'
                                             // must match pattern '^[a-z]([a-zA-Z]+)*$'

  public boolean myMethod(String sentence) {
    return Stream.of(sentence.split(" "))
            .map(word -> word.trim()) // OK
            .anyMatch(Word -> "in".equals(Word)); // violation, name 'Word'
                                   // must match pattern '^[a-z]([a-zA-Z]+)*$'
  }
}
        

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

LocalFinalVariableName

Since Checkstyle 3.0

Description

Checks that local final variable names conform to a specified pattern. A catch parameter and resources in try statements are considered to be a local, final variables.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
tokens tokens to check subset of tokens VARIABLE_DEF , PARAMETER_DEF , RESOURCE . VARIABLE_DEF , PARAMETER_DEF , RESOURCE . 3.0

Examples

To configure the check:

<module name="LocalFinalVariableName"/>
        

An example of how to configure the check for names that are only upper case letters and digits is:

<module name="LocalFinalVariableName">
  <property name="format" value="^[A-Z][A-Z0-9]*$"/>
</module>
        

Code Example:

class MyClass {
  void MyMethod() {
    try {
      final int VAR1 = 5; // OK
      final int var1 = 10; // violation,  name 'var1' must match pattern "^[A-Z][A-Z0-9]*$"
    } catch (Exception ex) {
      final int VAR2 = 15; // OK
      final int var2 = 20; // violation,  name 'var2' must match pattern "^[A-Z][A-Z0-9]*$"
    }
  }
}
        

An example of how to configure the check for names of local final parameters and resources in try statements (without checks on variables):

<module name="LocalFinalVariableName">
  <property name="format" value="^[A-Z][A-Z0-9]*$"/>
  <property name="tokens" value="PARAMETER_DEF,RESOURCE"/>
</module>
        

Code Example:

class MyClass {
  void MyMethod() {
    try(Scanner scanner = new Scanner()) { // violation, name 'scanner' must
                                           // match pattern '^[A-Z][A-Z0-9]*$'
      final int VAR1 = 5; // OK
      final int var1 = 10; // OK
    } catch (final Exception ex) { // violation, name 'ex'
                                   // must match pattern '^[A-Z][A-Z0-9]*$'
      final int VAR2 = 15; // OK
      final int var2 = 20; // OK
    }
  }
}
        

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

LocalVariableName

Since Checkstyle 3.0

Description

Checks that local, non-final variable names conform to a specified pattern. A catch parameter is considered to be a local variable.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
allowOneCharVarInForLoop Allow one character variable name in initialization expressions in FOR loop if one char variable name is prohibited by {@code format} regexp. boolean false 5.8

Examples

To configure the check:

<module name="LocalVariableName"/>
        

Code Example:

class MyClass {
  void MyMethod() {
    for (int var = 1; var < 10; var++) {} // OK
    for (int VAR = 1; VAR < 10; VAR++) {} // violation, name 'VAR' must match
                                          // pattern '^[a-z][a-zA-Z0-9]*$'
    for (int i = 1; i < 10; i++) {} // OK
    for (int var_1 = 0; var_1 < 10; var_1++) {} // violation, name 'var_1' must match
                                                   // pattern '^[a-z][a-zA-Z0-9]*$'
  }
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores is:

<module name="LocalVariableName">
  <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
</module>
        

Code Example:

class MyClass {
  void MyMethod() {
    for (int var = 1; var < 10; var++) {} // OK
    for (int VAR = 1; VAR < 10; VAR++) {} // violation, name 'VAR' must match
                                             // pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
    for (int i = 1; i < 10; i++) {} // OK
    for (int var_1 = 0; var_1 < 10; var_1++) {} // OK
  }
}
        

An example of one character variable name in initialization expression(like "i") in FOR loop:

for(int i = 1; i < 10; i++) {}
for(int K = 1; K < 10; K++) {}
List list = new ArrayList();
for (Object o : list) {}
for (Object O : list) {}
        

An example of how to configure the check to allow one character variable name in initialization expressions in FOR loop, where regexp allows 2 or more chars:

<module name="LocalVariableName">
  <property name="format" value="^[a-z][_a-zA-Z0-9]+$"/>
  <property name="allowOneCharVarInForLoop" value="true"/>
</module>
        

Code Example:

class MyClass {
  void MyMethod() {
    int good = 1;
    int g = 0; // violation
    for (int v = 1; v < 10; v++) { // OK
        int a = 1; // violation
    }
    for (int V = 1; V < 10; V++) { // OK
        int I = 1; // violation
    }
    List list = new ArrayList();
    for (Object o : list) { // OK
        String a = ""; // violation
    }
    for (Object O : list) { // OK
        String A = ""; // violation
    }
  }
}
        

An example of how to configure the check to that all variables have 3 or more chars in name:

<module name="LocalVariableName">
  <property name="format" value="^[a-z][_a-zA-Z0-9]{2,}$"/>
</module>
        

Code Example:

class MyClass {
  void MyMethod() {
    int goodName = 0;
    int i = 1; // violation
    for (int var = 1; var < 10; var++) { //OK
      int j = 1; // violation
    }
  }
}
        

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

MemberName

Since Checkstyle 3.0

Description

Checks that instance variable names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
applyToPublic Controls whether to apply the check to public member. boolean true 3.4
applyToProtected Controls whether to apply the check to protected member. boolean true 3.4
applyToPackage Controls whether to apply the check to package-private member. boolean true 3.4
applyToPrivate Controls whether to apply the check to private member. boolean true 3.4

Examples

To configure the check:

<module name="MemberName"/>
        

Code Example:

class MyClass {
  public int num1; // OK
  protected int num2; // OK
  final int num3 = 3; // OK
  private int num4; // OK

  static int num5; // ignored: not an instance variable
  public static final int CONSTANT = 1; // ignored: not an instance variable

  public int NUM1; // violation, name 'NUM1'
                   // must match pattern '^[a-z][a-zA-Z0-9]*$'
  protected int NUM2; // violation, name 'NUM2'
                      // must match pattern '^[a-z][a-zA-Z0-9]*$'
  final int NUM3; // violation, name 'NUM3'
                  // must match pattern '^[a-z][a-zA-Z0-9]*$'
  private int NUM4; // violation, name 'NUM4'
                    // must match pattern '^[a-z][a-zA-Z0-9]*$'
}
        

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="MemberName">
  <property name="format" value="^m[A-Z][a-zA-Z0-9]*$"/>
  <property name="applyToProtected" value="false"/>
  <property name="applyToPackage" value="false"/>
</module>
        

Code Example:

class MyClass {
  public int num1; // violation, name 'num1'
                   // must match pattern '^m[A-Z][a-zA-Z0-9]*$'
  protected int num2; // OK
  int num3; // OK
  private int num4; // violation, name 'num4'
                    // must match pattern '^m[A-Z][a-zA-Z0-9]*$'
}
        

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

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

Code Example:

class MyClass {
  public int NUM1; // OK
  protected int NUM2; // violation, name 'NUM2'
                      // must match pattern '^[a-z][a-zA-Z0-9]*$'
  int NUM3; // violation, name 'NUM3'
            // must match pattern '^[a-z][a-zA-Z0-9]*$'
  private int NUM4; // OK
}
        

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

MethodName

Since Checkstyle 3.0

Description

Checks that method names conform to a specified pattern.

Also, checks if a method name has the same name as the residing class. The default is false (it is not allowed). It is legal in Java to have method with the same name as a class. As long as a return type is specified it is a method and not a constructor which it could be easily confused as. Does not check-style the name of an overridden methods because the developer does not have a choice in renaming such methods.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
allowClassName Controls whether to allow a method name to have the same name as the residing class name. This is not to be confused with a constructor. An easy mistake is to place a return type on a constructor declaration which turns it into a method. For example:
class MyClass {
    public void MyClass() {} //this is a method
    public MyClass() {} //this is a constructor
}
                  
boolean false 5.0
applyToPublic Controls whether to apply the check to public member. boolean true 5.1
applyToProtected Controls whether to apply the check to protected member. boolean true 5.1
applyToPackage Controls whether to apply the check to package-private member. boolean true 5.1
applyToPrivate Controls whether to apply the check to private member. boolean true 5.1

Examples

To configure the check:

<module name="MethodName"/>
        

Code Example:

class MyClass {
  public void firstMethod1() {} // OK
  protected void secondMethod() {} // OK
  private void ThirdMethod() {} // violation, method name must match to the
                                // default pattern '^[a-z][a-zA-Z0-9]*$'
  public void fourth_Method4() {} // violation, method name must match to the
                                 // default pattern '^[a-z][a-zA-Z0-9]*$'
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores is:

<module name="MethodName">
   <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
</module>
        

Code Example:

class MyClass {
  public void myMethod() {} // OK
  public void MyMethod() {} // violation, name "MyMethod"
                            // should match the pattern "^[a-z](_?[a-zA-Z0-9]+)*$"
}
        

An example of how to configure the check to allow method names to be equal to the residing class name is:

<module name="MethodName">
   <property name="format" value="^[a-zA-Z](_?[a-zA-Z0-9]+)*$"/>
   <property name="allowClassName" value="true"/>
</module>
        

Code Example:

class MyClass {
  public MyClass() {} // OK
  public void MyClass() {} // OK, method Name 'MyClass' is allowed to be
                           // equal to the enclosing class name
}
        

An example of how to configure the check to disallow method names to be equal to the residing class name is:

<module name="MethodName">
   <property name="format" value="^[a-zA-Z](_?[a-zA-Z0-9]+)*$"/>
   <property name="allowClassName" value="false"/>
</module>
        

Code Example:

class MyClass {
  public MyClass() {} // OK
  public void MyClass() {} // violation,  method Name 'MyClass' must not
                           // equal the enclosing class name
}
        

An example of how to suppress the check to public and protected methods:

<module name="MethodName">
   <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
   <property name="applyToPublic" value="false"/>
   <property name="applyToProtected" value="false"/>
</module>
        

Code Example:

class MyClass {
  public void FirstMethod() {} // OK
  protected void SecondMethod() {} // OK
  private void ThirdMethod() {} // violation, name 'ThirdMethod' must match
                                // pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
  void FourthMethod() {} // violation, name 'FourthMethod' must match
                         // pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
}
        

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

MethodTypeParameterName

Since Checkstyle 5.0

Description

Checks that method type parameter names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[A-Z]$" 5.0

Examples

To configure the check:

<module name="MethodTypeParameterName"/>
        

Code Example:

class MyClass {
  public <T> void method1() {} // OK
  public <a> void method2() {} // violation,  name 'a' must match pattern '^[A-Z]$'
  public <K, V> void method3() {} // OK
  public <k, V> void method4() {} // violation, name 'k' must match pattern '^[A-Z]$'
}
        

An example of how to configure the check for names that are only a single letter is:

<module name="MethodTypeParameterName">
   <property name="format" value="^[a-zA-Z]$"/>
</module>
        

Code Example:

class MyClass {
  public <T> void method1() {} // OK
  public <a> void method2() {} // OK
  public <K, V> void method3() {} // OK
  public <k, V> void method4() {} // OK
}
        

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

PackageName

Since Checkstyle 3.0

Description

Checks that package names conform to a specified pattern.

The default value of format for module PackageName has been chosen to match the requirements in the Java Language specification and the Sun coding conventions. However both underscores and uppercase letters are rather uncommon, so most configurations should probably assign value ^[a-z]+(\.[a-z][a-z0-9]*)*$ to format for module PackageName.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$" 3.0

Examples

To configure the check:

<module name="PackageName"/>
        

Code Example:

package com; // OK
package COM; // violation, name 'COM' must match pattern '^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$'
package com.checkstyle.checks; // OK
package com.A.checkstyle.checks; // OK
package com.checkstyle1.checks; // OK
package com.checkSTYLE.checks; // OK
package com._checkstyle.checks_; // OK
        

An example of how to configure the check to ensure with packages start with a lowercase letter and only contains lowercase letters or numbers is:

<module name="PackageName">
  <property name="format"
    value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
</module>
        

Code Example:

package com; // OK
package COM; // violation, name 'COM' must match pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
package com.checkstyle.checks; // OK
package com.A.checkstyle.checks; // violation, name 'com.A.checkstyle' must match
                                 // pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
package com.checkstyle1.checks; // OK
package com.checkSTYLE.checks; // violation, name 'com.checkSTYLE.checks' must
                               // match pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
package com._checkstyle.checks_; // violation, name 'com._checkstyle.checks_' must match
                                 // pattern '^[a-z]+(\.[a-z][a-z0-9]*)*$'
        

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

ParameterName

Since Checkstyle 3.0

Description

Checks that method parameter names conform to a specified pattern. By using accessModifiers property it is possible to specify different formats for methods at different visibility levels.

To validate catch parameters please use CatchParameterName.

To validate lambda parameters please use LambdaParameterName.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
ignoreOverridden Allows to skip methods with Override annotation from validation. boolean false 6.12.1
accessModifiers Access modifiers of methods where parameters are checked. AccessModifierOption[] public, protected, package, private 7.5

Examples

To configure the check:

<module name="ParameterName"/>
        

Code Example:

class MyClass {
  void method1(int v1) {} // OK
  void method2(int V2) {} // violation, name 'V2' must match pattern '^[a-z][a-zA-Z0-9]*$'
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores:

<module name="ParameterName">
  <property name="format" value="^[a-z][_a-zA-Z0-9]+$"/>
</module>
        

Code Example:

class MyClass {
  void method1(int v1) {} // OK
  void method2(int v_2) {} // OK
  void method3(int V3) {} // violation, name 'V3' must match pattern '^[a-z][_a-zA-Z0-9]+$'
}
        

An example of how to configure the check to skip methods with Override annotation from validation:

<module name="ParameterName">
  <property name="ignoreOverridden" value="true"/>
</module>
        

Code Example:

class MyClass {
  void method1(int v1) {} // OK
  void method2(int V2) {} // violation, name 'V2' must match pattern '^[a-z][a-zA-Z0-9]*$'
  @Override
  public boolean equals(Object V3) { // OK
    return true;
  }
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters and digits is:

<module name="ParameterName">
  <property name="format" value="^[a-z][a-zA-Z0-9]+$"/>
</module>
        

Code Example:

class MyClass {
  void method1(int v1) {} // OK
  void method2(int v_2) {} // violation, name 'v_2' must match pattern '^[a-z][a-zA-Z0-9]+$'
  void method3(int V3) {} // violation, name 'V3' must match pattern '^[a-z][a-zA-Z0-9]+$'
}
        

The following configuration checks that the parameters always start with two lowercase characters and, in addition, that public method parameters cannot be one character long:

<module name="ParameterName">
  <property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
  <property name="accessModifiers" value="protected, package, private"/>
  <message key="name.invalidPattern"
    value="Parameter name ''{0}'' must match pattern ''{1}''"/>
</module>
<module name="ParameterName">
  <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
  <property name="accessModifiers" value="public"/>
  <message key="name.invalidPattern"
    value="Parameter name ''{0}'' must match pattern ''{1}''"/>
</module>
        

Code Example:

class MyClass {
  void method1(int v1) {} // OK
  protected method2(int V2) {} // violation, Parameter name 'V2'
                               // must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'
  private method3(int a) {} // OK
  public method4(int b) {} // violation, Parameter name 'b'
                           // must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'
}
        

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

PatternVariableName

Since Checkstyle 8.36

Description

Checks that pattern variable names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 8.36

Examples

To configure the check:

<module name="PatternVariableName"/>
        

Code Example:

class MyClass {
    MyClass(Object o1){
        if (o1 instanceof String STRING) { // violation, name 'STRING' must
        // match pattern '^[a-z][a-zA-Z0-9]*$'
        }
        if (o1 instanceof Integer num) { // OK
        }
    }
}
        

An example of how to configure the check for names that have a lower case letter, followed by letters and digits, optionally separated by underscore:

<module name="PatternVariableName">
    <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
</module>
        

Code Example:

class MyClass {
    MyClass(Object o1){
        if (o1 instanceof String STR) { // violation, name 'STR' must
        // match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
        }
        if (o1 instanceof Integer num) { // OK
        }
        if (o1 instanceof Integer num_1) { // OK
        }
    }
}
        

An example of how to configure the check to that all variables have 3 or more chars in name:

<module name="PatternVariableName">
    <property name="format" value="^[a-z][_a-zA-Z0-9]{2,}$"/>
</module>
        

Code Example:

class MyClass {
    MyClass(Object o1){
        if (o1 instanceof String s) { // violation, name 's' must
        // match pattern '^[a-z][_a-zA-Z0-9]{2,}$'
        }
        if (o1 instanceof Integer num) { // OK
        }
    }
}
        

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

RecordComponentName

Since Checkstyle 8.40

Description

Checks that record component names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 8.40

Examples

To configure the check:

<module name="RecordComponentName"/>
        

Example:

record MyRecord1(String value, int otherComponentName) {} // OK
record MyRecord2(String... Values) {} // violation, the record component name
                                    // should match the regular expression "^[a-z][a-zA-Z0-9]*$"
record MyRecord3(double my_number) {} // violation, the record component name
                                    // should match the regular expression "^[a-z][a-zA-Z0-9]*$"
        

An example of how to configure the check for names that are only letters in lowercase:

Configuration:

<module name="RecordComponentName">
    <property name="format" value="^[a-z]+$"/>
</module>
        

Example:

record MyRecord1(String value, int other) {} // OK
record MyRecord2(String... strings) {} // OK
record MyRecord3(double myNumber) {} // violation, the record component name
                             // should match the regular expression "^[a-z]+$"
        

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

RecordTypeParameterName

Since Checkstyle 8.36

Description

Checks that record type parameter names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[A-Z]$" 8.36

Examples

To configure the check:

<module name="RecordTypeParameterName"/>
        

An example of how to configure the check for names that are only a single letter is:

Configuration:

<module name="RecordTypeParameterName">
    <property name="format" value="^[a-zA-Z]$"/>
</module>
        

Example:

record MyRecord1<T> {} // OK
record MyRecord2<t> {} // OK
record MyRecord3<abc> {} // violation, the record type parameter
// name should match the regular expression "^[a-zA-Z]$"
        

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

StaticVariableName

Since Checkstyle 3.0

Description

Checks that static, non-final variable names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[a-z][a-zA-Z0-9]*$" 3.0
applyToPublic Controls whether to apply the check to public member. boolean true 5.0
applyToProtected Controls whether to apply the check to protected member. boolean true 5.0
applyToPackage Controls whether to apply the check to package-private member. boolean true 5.0
applyToPrivate Controls whether to apply the check to private member. boolean true 5.0

Examples

To configure the check:

<module name="StaticVariableName"/>
        

Code Example:

class MyClass {
  public static int goodStatic = 2; // OK
  private static int BadStatic = 2; // violation, name 'BadStatic'
                                    // must match pattern '^[a-z][a-zA-Z0-9]*$'
}
        

An example of how to suppress the check to public and protected types is:

<module name="StaticVariableName">
  <property name="applyToPublic" value="false"/>
  <property name="applyToProtected" value="false"/>
</module>
        

Code Example:

class MyClass {
  public static int GoodStatic1 = 2; // OK
  protected static int GoodStatic2 = 2; //OK
  private static int goodStatic = 2 // OK
  private static int BadStatic = 2; // violation, name 'BadStatic'
                                    // must match pattern '^[a-z][a-zA-Z0-9]*$'
}
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores. Also, suppress the check from being applied to private and package-private types:

<module name="StaticVariableName">
  <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
  <property name="applyToPrivate" value="false"/>
  <property name="applyToPackage" value="false"/>
</module>
        

Code Example:

class MyClass {
  public static int good_static = 2; // OK
  public static int Bad_Static = 2; // violation, name 'Bad_Static'
                                    // must match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
  private static int Good_Static1 = 2; // OK
  static int Good_Static2 = 2; // OK
}
        

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

TypeName

Since Checkstyle 3.0

Description

Checks that type names conform to a specified pattern.

Properties

name description type default value since
format Specifies valid identifiers. Pattern "^[A-Z][a-zA-Z0-9]*$" 3.0
applyToPublic Controls whether to apply the check to public member. boolean true 5.0
applyToProtected Controls whether to apply the check to protected member. boolean true 5.0
applyToPackage Controls whether to apply the check to package-private member. boolean true 5.0
applyToPrivate Controls whether to apply the check to private member. boolean true 5.0
tokens tokens to check subset of tokens CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , RECORD_DEF . CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , RECORD_DEF . 3.0

Examples

To configure the check:

<module name="TypeName"/>
        

Code Example:

public interface FirstName {} // OK
protected class SecondName {} // OK
enum Third_Name {} // violation, name 'Third_Name' must match pattern '^[A-Z][a-zA-Z0-9]*$'
private class FourthName_ {} // violation, name 'FourthName_'
                            // must match pattern '^[A-Z][a-zA-Z0-9]*$'
        

An example of how to configure the check for names that begin with a lower case letter, followed by letters, digits, and underscores. Also, suppress the check from being applied to protected and private type:

<module name="TypeName">
  <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
  <property name="applyToProtected" value="false"/>
  <property name="applyToPrivate" value="false"/>
</module>
        

Code Example:

public interface firstName {} // OK
public class SecondName {} // violation, name 'SecondName'
                          // must match pattern '^[a-z](_?[a-zA-Z0-9]+)*$'
protected class ThirdName {} // OK
private class FourthName {} // OK
        

The following configuration element ensures that interface names begin with "I_", followed by letters and digits:

<module name="TypeName">
  <property name="format"
    value="^I_[a-zA-Z0-9]*$"/>
  <property name="tokens"
    value="INTERFACE_DEF"/>
</module>
        

Code Example:

public interface I_firstName {} // OK
interface SecondName {} // violation, name 'SecondName'
                       // must match pattern '^I_[a-zA-Z0-9]*$'
        

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