What is variable shadowing used for in a Java clas

2018-12-31 20:46发布

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);
    }
}

5条回答
后来的你喜欢了谁
2楼-- · 2018-12-31 21:18

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

查看更多
若你有天会懂
3楼-- · 2018-12-31 21:24

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.

查看更多
有味是清欢
4楼-- · 2018-12-31 21:24

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.

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 21:25

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.

查看更多
人气声优
6楼-- · 2018-12-31 21:27

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.)

查看更多
登录 后发表回答