What is variable shadowing used for in a Java class?

后端 未结 5 1365
春和景丽
春和景丽 2020-11-22 14:53

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 Ja

相关标签:
5条回答
  • 2020-11-22 14:55

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

    0 讨论(0)
  • 2020-11-22 14:57

    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.

    0 讨论(0)
  • 2020-11-22 15:01

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

    0 讨论(0)
  • 2020-11-22 15:05

    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.

    0 讨论(0)
  • 2020-11-22 15:05

    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.

    0 讨论(0)
提交回复
热议问题