Java Reference
In-Depth Information
means that the copy of
val
associated with the invoking object will be returned. However,
the same statement can also be written like this:
Here,
this
refers to the object on which
get_pwr( )
was called. Thus,
this.val
refers to that
object's copy of
val
. For example, if
get_pwr( )
had been invoked on
x
, then
this
in the
preceding statement would have been referring to
x
. Writing the statement without using
this
is really just shorthand.
Here is the entire
Pwr
class written using the
this
reference:
Actually, no Java programmer would write
Pwr
as just shown because nothing is gained,
and the standard form is easier. However,
this
has some important uses. For example, the
Java syntax permits the name of a parameter or a local variable to be the same as the name
of an instance variable. When this happens, the local name
hides
the instance variable. You