I'm reading my Deitel, Java How to Program book and came across the term shadowing. If shadowing is allowed, what situation or what purpose is there for it in a Java class?
Example:
public class Foo {
int x = 5;
public void useField() {
System.out.println(this.x);
}
public void useLocal() {
int x = 10;
System.out.println(x);
}
}
One major purpose is to confuse people. It's bad practice and should be avoided.
It can be useful for setters where you don't want to have to create a separate variable name just for the method parameter eg:
Apart from that I'd avoid them.
Shadowing is not really a java only term. In any instance where a variable declared in a scope has the same name as one in a bigger scope, that variable is shadowed.
Some common uses for shadowing is when you have inner and outer classes and want to maintain a variable with the same name.
If you can avoid it though, you should since it may cause confusion.
The basic purpose of shadowing is to decouple the local code from the surrounding class. If it wasn't available, then consider the following case.
A Class Foo in an API is released. In your code you subclass it, and in your subclass use a variable called bar. Then Foo releases an update and adds a protected variable called Bar to its class.
Now your class won't run because of a conflict you could not anticipate.
However, don't do this on purpose. Only let this happen when you really don't care about what is happening outside the scope.
The two common uses are constructors and set methods:
Very occassionally it's useful if you want a copy of the variable at a single instant, but the variable may change within the method call.
(Of course, a contrived example made unnecessary with the posh for loop.)