Java Reference
In-Depth Information
S ELF C HECK
6. Is the substring method of the String class an accessor or a
mutator?
7. Is the Rectangle class immutable?
8.4 Side Effects
A mutator method modifies the object on which it is invoked, whereas an accessor
method leaves it unchanged. This classification relates only to the object on which the
method is invoked.
341
342
A side effect of a method is any kind of modification of data that is observable outside
the method. Mutator methods have a side effect, namely the modification of the
implicit parameter.
A side effect of a method is any externally observable data modification.
Here is an example of a method with another kind of side effect, the updating of an
explicit parameter:
public class BankAccount
{
/**
Transfers money from this account to another
account.
@param amount the amount of money to transfer
@param other the account into which to
transfer the money
*/
public void transfer(double amount, BankAccount
other)
{
balance = balance - amount;
other.balance = other.balance + amount;
}
. . .
}
Search WWH ::




Custom Search