Java Reference
In-Depth Information
This doesn't work. Let's consider a method call.
double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance);
System.out.println(savingsBalance);
As the method starts, the parameter variable otherBalance is set to the same
value as savingsBalance . Then the value of the otherBalance value is
modified, but that modification has no effect on savingsBalance , because
otherBalance is a separate variable (see Figure 3 ). When the method
terminates, the otherBalance variable dies, and savingsBalance isn't
increased.
In Java, a method can never change parameters of primitive type.
Why did the example at the beginning of Section 8.4 work, where the second
explicit parameter was a BankAccount reference? Then the parameter variable
contained a copy of the object reference. Through that reference, the method is
able to modify the object.
343
Search WWH ::




Custom Search