Possible confusion of local and field¶
ID: java/local-shadows-fieldKind: problemSecurity severity: Severity: recommendationPrecision: highTags: - quality - maintainability - readabilityQuery suites: - java-security-and-quality.qls
Click to see the query in the CodeQL repository
If a method declares a local variable with the same name as a field, then it is very easy to mix up the two when reading or modifying the program.
Recommendation¶
Consider using different names for the field and local variable to make the difference between them clear.
Example¶
The following example shows a local variablevalues that has the same name as a field.
publicclassContainer{privateint[]values;// Field called 'values'publicContainer(int...values){this.values=values;}publicContainerdup(){intlength=values.length;int[]values=newint[length];// Local variable called 'values'Containerresult=newContainer(values);returnresult;}}
References¶
Help - Eclipse Platform:Java Compiler Errors/Warnings Preferences.
Java Language Specification: 6.4 Shadowing and Obscuring.