FinalLocalVariable

Since Checkstyle 3.2

Description

Checks that local variables that never have their values changed are declared final. The check can be configured to also check that unchanged parameters are declared final.

Notes

When configured to check parameters, the check ignores parameters of interface methods and abstract methods.

Properties

name description type default value since
validateEnhancedForLoopVariable Control whether to check enhanced for-loop variable. boolean false 6.5
validateUnnamedVariables Control whether to check unnamed variables. boolean false 10.18.0
tokens tokens to check subset of tokens VARIABLE_DEF , PARAMETER_DEF . VARIABLE_DEF . 3.2

Examples

To configure the check:

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

Example:

class Example1
{
  static int foo(int x, int y) {
    //ok, because PARAMETER_DEF is not configured in tokens
    return x+y;
  }
  public static void main (String []args) {
    //ok above, because PARAMETER_DEF is not configured in tokens
    //ok below, because validateEnhancedForLoopVariable is false by default
    for (String i : args) {
      System.out.println(i);
    }
    int result=foo(1,2); // violation, 'Variable 'result' should be declared final'
  }
}
        

To configure the check so that it checks local variables and parameters:

<module name="Checker">
  <module name="TreeWalker">
    <module name="FinalLocalVariable">
      <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
    </module>
  </module>
</module>
        

Example:

class Example2
{
  static int foo(int x, int y) {
    // 2 violations above:
    //  'Variable 'x' should be declared final'
    //  'Variable 'y' should be declared final'
    return x+y;
  }
  public static void main (String []args) {
    // violation above, 'Variable 'args' should be declared final'
    // ok below, because validateEnhancedForLoopVariable is false by default
    for (String i : args) {
      System.out.println(i);
    }
    int result=foo(1,2); // violation, 'Variable 'result' should be declared final'
  }
}
        

By default, this Check skip final validation on Enhanced For-Loop.

Option 'validateEnhancedForLoopVariable' could be used to make Check to validate even variable from Enhanced For Loop.

An example of how to configure the check so that it also validates enhanced For Loop Variable is:

<module name="Checker">
  <module name="TreeWalker">
    <module name="FinalLocalVariable">
      <property name="tokens" value="VARIABLE_DEF"/>
      <property name="validateEnhancedForLoopVariable" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example3
{
  static int foo(int x, int y) {
    //ok, because PARAMETER_DEF is not configured in tokens
    return x+y;
  }
  public static void main (String []args) {
    // ok above, because PARAMETER_DEF is not configured in tokens
    // violation below, 'Variable 'i' should be declared final'
    for (String i : args) {
      System.out.println(i);
    }
    int result=foo(1,2); // violation, 'Variable 'result' should be declared final'
  }
}
        

An example of how to configure check on local variables and parameters but do not validate loop variables:

<module name="Checker">
  <module name="TreeWalker">
    <module name="FinalLocalVariable">
      <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
      <property name="validateEnhancedForLoopVariable" value="false"/>
    </module>
  </module>
</module>
        

Example:

class Example4
{
  static int foo(int x, int y) {
    // 2 violations above:
    //  'Variable 'x' should be declared final'
    //  'Variable 'y' should be declared final'
    return x+y;
  }
  public static void main (String []args) {
    // violation above, 'Variable 'args' should be declared final'
    // ok below, because validateEnhancedForLoopVariable is false by default
    for (String i : args) {
      System.out.println(i);
    }
    int result=foo(1,2); // violation, 'Variable 'result' should be declared final'
  }
}
        

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

Parent Module

TreeWalker