Class FinalLocalVariableCheck

  • All Implemented Interfaces:
    Configurable, Contextualizable

    public class FinalLocalVariableCheck
    extends AbstractCheck

    Ensures that local variables that never get their values changed, must be declared final.

    An example of how to configure the check to validate variable definition is:

     <module name="FinalLocalVariable">
         <property name="tokens" value="VARIABLE_DEF"/>
     </module>
     

    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="FinalLocalVariable">
         <property name="tokens" value="VARIABLE_DEF"/>
         <property name="validateEnhancedForLoopVariable" value="true"/>
     </module>
     

    Example:

    for (int number : myNumbers) { // violation System.out.println(number); }

    Author:
    k_gibbs, r_auckenthaler, Vladislav Lisetskiy