Java Reference
In-Depth Information
public class BankAccount
{
public void transfer(double amount, BankAccount
other)
{
balance = balance - amount; // i.e., this. balance
other.balance = other.balance + amount;
}
. . .
}
Here, the unqualified name balance means this.balance . (Recall from
Chapter 3 that this is a reference to the implicit parameter of any method.)
The same rule applies to methods. Thus, another implementation of the transfer
method is
public class BankAccount
{
public void transfer(double amount, BankAccount
other)
{
withdraw(amount); // i.e., this. withdraw(amount);
other.deposit(amount);
}
. . .
}
Whenever you see an instance method call without an implicit parameter, then the
method is called on the this parameter. Such a method call is called a Ȓself-callȓ.
359
360
Similarly, you can use a static field or method of the same class without a qualifier.
For example, consider the following version of the withdraw method:
public class BankAccount
{
public void withdraw(double amount)
{
if (balance < amount) balance = balance -
OVERDRAFT_FEE;
else . . .
}
. . .
Search WWH ::




Custom Search