...
The resulting behavior can be classified as shadowing; the method variable renders the class instance variable inaccessible within the scope of the method. For example, assigning to this.val
from within the method does not affect the value of the call instance variable, although assigning to this.val
from within the method does.
Compliant Solution (Field Shadowing)
...
Name reuse makes code more difficult to read and maintain, which can result in security weaknesses. An automated tool can easily detect reuse of identifiers in containing scopes.
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.DCL51.HMF | Do not give method local variables and parameters the same name as class fields | ||||||
SonarQube |
| HiddenFieldCheck |
Bibliography
Puzzle 67, "All Strung Out" | |
Item 16, "Prefer Interfaces to Abstract Classes" | |
§6.3, "Placement" | |
[JLS 2013] | §6.4.1, "Shadowing" |
...
...