Skip to content

Files

Latest commit

 

History

History
55 lines (32 loc) · 1.31 KB

FinalLocalVariable.md

File metadata and controls

55 lines (32 loc) · 1.31 KB

Pattern: Local variable not declared as final

Issue: -

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.

Examples

To configure the check:

<module name="FinalLocalVariable"/>

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

<module name="FinalLocalVariable">
    <property name="tokens" value="VARIABLE_DEF,PARAMETER_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);
}

Further Reading