No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Name Shadowing
Member variables can be shadowed by local variables—in particular function arguments. This allows methods and constructors of the standard Java form:
1Public Class Shadow {
2 String s;
3 Shadow(String s) { this.s = s; } // Same name ok
4 setS(String s) { this.s = s; } // Same name ok
5}Member variables in one class can shadow member variables with the same name in a parent classes. This can be useful if the two classes are in different top-level classes and written by different teams. For example, if one has a reference to a class C and wants to gain access to a member variable M in parent class P (with the same name as a member variable in C) the reference should be assigned to a reference to P first.
Static variables can be shadowed across the class hierarchy—so if P defines a static S, a subclass C can also declare a static S. References to S inside C refer to that static—in order to reference the one in P, the syntax P.S must be used.
Static class variables cannot be referenced through a class instance. They must be referenced using the raw variable name by itself (inside that top-level class file) or prefixed with the class name. For example:
1public class p1 {
2 public static final Integer CLASS_INT = 1;
3 public class c { };
4}
5p1.c c = new p1.c();
6// This is illegal
7// Integer i = c.CLASS_INT;
8// This is correct
9Integer i = p1.CLASS_INT;