Java Reference
In-Depth Information
9.6. Member Access
You use the dot ( . ) operatoras in ref.method() to access both instance
members or static members of types. Because types can inherit members
from their supertypes, there are rules regarding which member is ac-
cessed in any given situation. Most of these rules were covered in detail
in Chapter 2 and Chapter 3 , but we briefly recap them.
You access static members by using either the type name or an object
reference. When you use a type name, the member referred to is the
member declared in that type (or inherited by it if there was no declara-
tion in that type). When you use an object reference, the declared type
of the reference determines which member is accessed, not the type of
the object being referred to. Within a class, reference to a static member
always refers to the member declared in, or inherited by, that class.
Non-static members are accessed through an object referenceeither an
explicit reference or implicitly this (or one of the enclosing objects) if the
non-static member is a member of the current object (or enclosing ob-
ject). Fields and nested types are accessed based on the declared type
of the object reference. Similarly, within a method, a reference to a field
or nested type always refers to the declaration within that class or else
the inherited declaration. In contrast, methods are accessed based on
the class of the object being referred to. Further, the existence of meth-
od overloading means that the system has to determine which method to
invoke based on the compile-time type of the arguments used in the in-
vocation; this process is described in detail in the next section. The only
operator that can be applied to a method member is the method invoca-
tion operator () .
You will get a NullPointerException if you use . on a reference with the
value null , unless you are accessing a static member. In that case the
value of the reference is never considered, because only the type of the
reference determines the class in which to locate the member.
 
Search WWH ::




Custom Search