...
- Reuse the name of a superclass
- Reuse the name of an interface
- Reuse the name of a field defined in a superclass
- Reuse the name of a field that appears in the same method (in some different scope)
- Reuse the name of a field, type or another parameter across packages
Wiki Markup |
---|
It is permissible to declare a label with the same name as another variable in the same scope. This is because there is no obscuration in this case \[[JLS 05|AA. Java References#JLS 05]\]. |
Noncompliant Code Example
...
Code Block |
---|
|
package x;
public class A {
void doLogic() {
// print 'A'
}
public static void main(String[] args) {
// explicitly invokes doSequence() of class y.C and prints 'C'
y.C.doSequence();
}
}
package x;
public class B { /* ... */ }
package y; // different package
public class C extends x.B {
public void doSequence() { // now renamed
// print 'C'
}
}
|
Exceptions
EX1: Reuse of names is permitted for trivial loop counter declarations in the same scope:
Code Block |
---|
|
for(int i = 0; i < 10; i++) { }
for(int i = 0; i < 20; i++) { }
|
Wiki Markup |
---|
*EX2:* It is permissible to declare a label with the same name as another variable in the same scope. This is because there is no obscuration in this case \[[JLS 05|AA. Java References#JLS 05]\]. |
Risk Assessment
Reusing names leads to code that is harder to read and maintain and may result in security weaknesses.
...