What is variable shadowing used for in a Java class?

后端 未结 5 1364
春和景丽
春和景丽 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: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.

提交回复
热议问题