Skip to main content

hidden and shadow in java

· 2 min read

shadow

这个词是描述同一个类里面不同作用范围内的同名变量,越里面的变量越覆盖越外面的同名变量

shadow 会和几个词相关:

  • scope
  • simple name

原则: 在使用simple name的情况下 , scope 里面的会shadow scope外面的

If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.

15.11. Field Access Expressions

6.2 Names and Identifiers A name is used to refer to an entity declared in a program. There are two forms of names: simple names and qualified names. A simple name is a single identifier. A qualified name consists of a name, a "." token, and an identifier

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is not shadowed (§6.4.1)

6.4.1 Shadowing Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.

hidden

hidden描述的父类和子类同名变量的可读性