Java Reference
In-Depth Information
343
S ELF C HECK
8. If a refers to a bank account, then the call a.deposit(100) modifies
the bank account object. Is that a side effect?
9. Consider the DataSet class of Chapter 6 . Suppose we add a method
void read(Scanner in)
{
while (in.hasNextDouble())
add(in.nextDouble());
}
Does this method have a side effect other than mutating the data set?
C OMMON E RROR 8.1: Trying to Modify Primitive Type
Parameters
Methods can't update parameters of primitive type (numbers, char , and
boolean ). To illustrate this point, let us try to write a method that updates a
number parameter:
public class BankAccount
{
/**
Transfers money from this account and tries to add it to a balance.
@param amount the amount of money to transfer
@param otherBalance balance to add the amount to
*/
void transfer(double amount, double
otherBalance)
{
balance = balance - amount;
otherBalance = otherBalance + amount;
// Won't work
}
. . .
}
Search WWH ::




Custom Search