Name Shadowing
A member variable can be shadowed by a local variable, especially a method parameter. The parameter can use the same name as the field. Use this to refer to the field, and the bare name to refer to the parameter.
A member variable in a subclass can shadow a member variable with the same name in a parent class. This can happen when the two classes are in different top-level classes and written by different teams. The compile-time type of the reference determines which field you get. To read the parent’s value, assign the object to a parent-class reference first.
Create this parent class.
Then create this subclass, which declares its own label.
This snippet shows the two fields. Run it after you create both classes.
Static variables can also be shadowed across the class hierarchy. If a parent class defines a static variable, a subclass can declare a static variable with the same name. Inside the subclass, the bare name refers to the subclass static. Prefix the name with the parent class to get the parent’s value.
Create this parent class.
Then create this subclass.
Static variables cannot be referenced through a class instance. Use the variable name by itself (inside that class) or prefix it with the class name.