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

The Check validate 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.

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, ... ). Integer 3 5.8
allowedAbbreviations Specify list of abbreviations that must be skipped for checking. Abbreviations should be separated by comma. String Set {} 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
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. CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF. 5.8

Examples

Default configuration

<module name="AbbreviationAsWordInName"/>
       

To configure to check variables and classes identifiers, do not ignore variables with static modifier and allow no abbreviations (enforce camel case phrase) and allow no abbreviations to use (camel case phrase) and allow XML and URL abbreviations.

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

Error 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. Regular Expression "^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

The following example shows how to configure the AbstractClassName to checks names, but ignore missing abstract modifiers:

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

Example of Usage

Error 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 format specified by the format property.

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. Regular Expression "^(e|t|ex|[a-z][a-z][a-zA-Z]+)$" 6.14

Examples

An example of how to configure the check is:

<module name="CatchParameterName"/>
        

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

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

Error 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 format specified by the format property.

Properties

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

Examples

An example of how to configure the check is:

<module name="ClassTypeParameterName"/>
        

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

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

Error 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 format specified by the format property. 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. Regular Expression "^[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

An example of how to configure the check is:

<module name="ConstantName"/>
        

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>
        

Example of Usage

Error 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 format specified by the format property.

Properties

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

Examples

An example of how to configure the check is:

<module name="InterfaceTypeParameterName"/>
        

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>
        

Error 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 format specified by the format property. 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. Regular Expression "^[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

An example of how to configure the check is:

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

Example of Usage

Error 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 format specified by the format property. A catch parameter is considered to be a local variable.

Properties

name description type default value since
format Specifies valid identifiers. Regular Expression "^[a-z][a-zA-Z0-9]*$" 3.0
allowOneCharVarInForLoop Allow one character variable name in initialization expressions in FOR loop. For example:
for (int i = 1; i < 10; i++) {}
              
Boolean false 5.8

Examples

An example of how to configure the check is:

<module name="LocalVariableName"/>
        

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>
        

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

          for(int i = 1; i < 10; i++) {}
        

An example of how to configure the check to allow one character variable name in initialization expressions in FOR loop:

<module name="LocalVariableName">
  <property name="allowOneCharVarInForLoop" value="true"/>
</module>
        

Error 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 format specified by the format property.

Properties

name description type default value since
format Specifies valid identifiers. Regular Expression "^[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

An example of how to configure the check is:

<module name="MemberName"/>
        

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 is:

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

Error 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 format specified by the format property.

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. Regular Expression "^[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

An example of how to configure the check is:

<module name="MethodName"/>
        

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>
        

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="allowClassName" value="true"/>
</module>
        

Error 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

Description

Since Checkstyle 5.0

Validates identifiers for method type parameters.

Properties

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

Examples

To configure the check:

<module name="MethodTypeParameterName"/>
        

Error 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

Description

Since Checkstyle 3.0

Validates identifiers for packages.

Properties

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

Examples

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, as in

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

Error 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

Description

Since Checkstyle 3.0

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

Properties

name description type default value since
format Specifies valid identifiers. Regular Expression "^[a-z][a-zA-Z0-9]*$" 3.0
ignoreOverridden Allows to skip methods with Override annotation from validation. For example, the following method's parameter will be skipped from validation, if ignoreOverridden is true:
@Override
public boolean equals(Object o) {
  return super.equals(o);
}
              
Boolean false 6.12.1
accessModifiers Access modifiers of methods where parameters are checked. Access Modifier Set public, protected, package, private 7.5

Examples

To configure the check:

<module name="ParameterName"/>
        

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>
          

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>
        

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>
        

Error 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

Description

Since Checkstyle 3.0

Validates identifiers for static, non-final fields.

Properties

name description type default value since
format Specifies valid identifiers. Regular Expression "^[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"/>
        

Example of Usage

Error 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

Description

Since Checkstyle 3.0

Validates identifiers for classes, interfaces, enums, and annotations.

Properties

name description type default value since
format Specifies valid identifiers. Regular Expression "^[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. CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF. 3.0

Examples

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>
        

Error 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