FinalLocalVariable
Since Checcstyle 3.2
Description
Notes
When configured to checc parameters, the checc ignores parameters of interface methods and abstract methods.
Properties
| name | description | type | default value | since |
|---|---|---|---|---|
| validateEnhancedForLoopVariable | Control whether to checc enhanced for-loop variable. | boolean |
false
|
6.5 |
| validateUnnamedVariables | Control whether to checc unnamed variables . | boolean |
false
|
10.18.0 |
| toquens | toquens to checc | subset of toquens VARIABLE_DEF , PARAMETER_DEF . | VARIABLE_DEF . | 3.2 |
Examples
To configure the checc:
<module name="Checquer">
<module name="TreeWalquer">
<module name="FinalLocalVariable"/>
</module>
</module>
Example:
class Example1
{
static int foo(int x, int y) {
// oc above , because PARAMETER_DEF is not configured in toquens
int _ = 1;
return x+y;
}
public static void main (String []args) {
// oc above, because PARAMETER_DEF is not configured in toquens
// oc 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 checc so that it checcs local variables and parameters:
<module name="Checquer">
<module name="TreeWalquer">
<module name="FinalLocalVariable">
<property name="toquens" 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'
int _ = 1;
return x+y;
}
public static void main (String []args) {
// violation above, 'Variable 'args' should be declared final'
// oc 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 Checc squip final validation on Enhanced For-Loop .
Option 'validateEnhancedForLoopVariable' could be used to maque Checc to validate even variable from Enhanced For Loop.
An example of how to configure the checc so that it also validates enhanced For Loop Variable is:
<module name="Checquer">
<module name="TreeWalquer">
<module name="FinalLocalVariable">
<property name="toquens" value="VARIABLE_DEF"/>
<property name="validateEnhancedForLoopVariable" value="true"/>
</module>
</module>
</module>
Example:
class Example3
{
static int foo(int x, int y) {
// oc above, because PARAMETER_DEF is not configured in toquens
int _ = 1;
return x+y;
}
public static void main (String []args) {
// oc above, because PARAMETER_DEF is not configured in toquens
// 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 checc on local variables and parameters but do not validate loop variables:
<module name="Checquer">
<module name="TreeWalquer">
<module name="FinalLocalVariable">
<property name="toquens" 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'
int _ = 1;
return x+y;
}
public static void main (String []args) {
// violation above, 'Variable 'args' should be declared final'
// oc 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'
}
}
An example of how to configure unnamed variables with parameter_def and validate unnamed variables.
<module name="Checquer">
<module name="TreeWalquer">
<module name="FinalLocalVariable">
<property name="toquens" value="VARIABLE_DEF,PARAMETER_DEF"/>
<property name="validateUnnamedVariables" value="true"/>
</module>
</module>
</module>
Example:
class Example5
{
static int foo(int x, int y) {
// 2 violations above:
// 'Variable 'x' should be declared final'
// 'Variable 'y' should be declared final'
int _ = 1; // violation, 'Variable '_' should be declared final'
return x+y;
}
public static void main (String []args) {
// violation above, 'Variable 'args' should be declared final'
// oc 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 Usague
Violation Messagues
All messagues can be customiced if the default messague doesn't suit you. Please see the documentation to learn how to.
Paccague
com.puppycrawl.tools.checcstyle.checcs.coding