What are Shadow Variables in Java?
1 answer
Instead of providing my own description, I may ask you to read about it, for example, here: http://en.wikipedia.org/wiki/Variable_shadowing . Once you understand variable shading, I recommend that you continue reading about overlay / shadow and visibility techniques in general to get a complete understanding of such terms.
In fact, since the question is asked in Java terms, here's a mini example:
public class Shadow {
private int myIntVar = 0;
public void shadowTheVar(){
// since it has the same name as above object instance field, it shadows above
// field inside this method
int myIntVar = 5;
// If we simply refer to 'myIntVar' the one of this method is found
// (shadowing a seond one with the same name)
System.out.println(myIntVar);
// If we want to refer to the shadowed myIntVar from this class we need to
// refer to it like this:
System.out.println(this.myIntVar);
}
public static void main(String[] args){
new Shadow().shadowTheVar();
}
}
+8
source to share