tags:

views:

557

answers:

5

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);
    }
}
+4  A: 

One major purpose is to confuse people. It's bad practice and should be avoided.

Robert Munteanu
Controversial I assume ;-)
Robert Munteanu
+7  A: 

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:

public void setX(int x) {
    this.x = x;
}

Apart from that I'd avoid them.

Tom
+2  A: 

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.

yx
+3  A: 

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.

Yishai
A: 

The two common uses are constructors and set methods:

public Foo(int x) {
    this.x = x;
}

public void setX(int x) {
    this.x = x;
}

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.

private void fire() {
    Listener[] listeners = this.listeners;
    int num = listeners.length;
    for (int ct=0; ct<num; ++ct) {
        listeners[ct].stateChanged();
    }
}

(Of course, a contrived example made unnecessary with the posh for loop.)

Tom Hawtin - tackline