Java Reference
In-Depth Information
. . .
}
However, you can have local variables with identical names if their scopes do not
overlap, such as
if (x >= 0)
{
double r = Math.sqrt(x);
. . .
} // Scope of r ends here
else
{
Rectangle r = new Rectangle(5, 10, 20, 30);
// OKȌit is legal to declare another r here
. . .
}
358
359
8.8.2 Scope of Class Members
In this section, we consider the scope of fields and methods of a class. (These are
collectively called the members of the class.) Private members have class scope:
You can access all members in any of the methods of the class.
A qualified name is prefixed by its class name or by an object reference, such as
Math.sqrt or other.balance .
If you want to use a public field or method outside its class, you must qualify the
name. You qualify a static field or method by specifying the class name, such as
Math.sqrt or Math.PI . You qualify an instance field or method by specifying
the object to which the field or method should be applied, such as
harrysChecking.getBalance() .
An unqualified instance field or method name refers to the this parameter.
Inside a method, you don't need to qualify fields or methods that belong to the same
class. Instance fields automatically refer to the implicit parameter of the method,
that is, the object on which the method is invoked. For example, consider the
transfer method:
Search WWH ::




Custom Search