Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^244) | File Objects and Looping Statements
Figure 5.8 The Effect of Assigning a New Value to a Reference Type Parameter
Figure 5.9 shows this process, and you should carefully compare it to Figure 5.8 to be sure
that you understand the difference. Assigning a new value (the address of a different object)
to a reference type parameter does not change the object to which the argument
refers. But changes to the fields of the object referred to by the parameter are
made to the argument object.
When you need to change the contents of an argument’s object, you should
make it clear in the documentation that the method changes the object. Software
engineers refer to a method that changes the state of a mutable object as a trans-
former. We will have more to say about this issue in Chapter 7.


A Parameter-Passing Analogy


To help you remember the behavior of simple and reference parameters, we offer another
analogy. Suppose you have a slip of paper in your hand, and you are talking to someone who
has another slip of paper. You are like the call and the other person is like the method. You
read to the person what’s written on your paper and he or she writes it down. If your slip con-
tains a simple type, such as a number representing the current temperature, the other party

Transformer A method that
changes the state of a mutable
object
Free download pdf