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
allowClassName Control whether to allow a method name to have the same name as the enclosing class name. Setting this property false helps to avoid confusion between constructors and methods. boolean false 5.0
applyToPackage Control if check should apply to package-private members. boolean true 5.1
applyToPrivate Control if check should apply to private members. boolean true 5.1
applyToProtected Control if check should apply to protected members. boolean true 5.1
applyToPublic Control if check should apply to public members. boolean true 5.1
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="MethodName"/>
  </module>
</module>
        

Code Example:

class Example1 {
  public void method1() {}
  protected void method2() {}
  private void Method3() {} // violation
  public void Method4() {} // violation
}
        

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="Checker">
  <module name="TreeWalker">
    <module name="MethodName">
       <property name="format" value="^[a-z](_?[a-zA-Z0-9]+)*$"/>
    </module>
  </module>
</module>
        

Code Example:

class Example2 {
  public void method1() {}
  public void Method2() {} // violation
}
        

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

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

Code Example:

class Example3 {
  public Example3() {}
  public void Example3() {}
}
        

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

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

Code Example:

class Example4 {
  public Example4() {}
  public void Example4() {} // violation
}
        

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

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

Code Example:

class Example5 {
  public void Method1() {}
  protected void Method2() {}
  private void Method3() {} // violation
  void Method4() {} // 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