What is variable shadowing used for in a Java class?

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

提交回复
热议问题