Scope Rules
Scope rules
are used to determine the specific object or method that is being referenced
when an identifier is used.
In Java, scope is determined by blocks of code (code within curly braces)
and the class hierarchy of the current instance.
When Java needs to resolve an issue of scope, it looks for an identifier's
definition in the following order (and stops looking when the identifier is
found):
- the current block
- surrounding blocks
- the class of the current instance (or current object)
- the superclasses of the current instance's class hierarchy
This means that a local object with the same name as an instance
variable will be found and used, not the instance variable.
It is a good idea to avoid using the names in a class or its superclass(es)
for local variables.
If you do have local variables with the same identifier names and want to
access the instance variables, use this.