 
                            ...
| Wiki Markup | 
|---|
| According to the Java Language Specification \[[JLS 2005|AA. Java References#JLS 05]\] section 6.3.2 "Obscured Declarations": | 
A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package.
This implies that a variable can obscure a type or a package, and a type can obscure a package name. Shadowing on the other hand refers to masking of variables, fields, types, method parameters, labels and exception handler parameters in a subscope. Both these differ from hiding wherein an accessible member (typically non-private) that should have been inherited by a subclass is forgone in lieu of a locally declared subclass member that assumes the same name.
As a tenet, do not:
- 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
...
Well defined import statements do resolve these issues but may get confusing when the reused name is defined in a different package. Moreover, a common (and misleading) tendency is to include the import statements after writing the code. (many Many IDEs allow automatic inclusion of import statements as per the requirements.) . This can create even more ambiguity with respect to the names because if a custom type is found in the same package, no further searches are conducted for the package names that must be imported.
...
| 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
SCP02-EX1: Reuse of names is permitted for trivial loop counter declarations in the same scope:
...
Reusing names leads to code that is harder to read and maintain and may result in security weaknesses.
| Rule Guideline | Severity | Likelihood | Remediation Cost | Priority | Level | 
|---|---|---|---|---|---|
| SCP02- J | low | unlikely | medium | P2 | L3 | 
...