
Provide a mutator method
for any instance variable
that you want to allow the
client to change.If the
argument sent to the
method is not a valid value
for the instance variable,
one option is for the muta-
tor method to print a mes-
sage to System.err and
leave the value of the
instance variable
unchanged.
SOFTWARE
ENGINEERING TIP
388 CHAPTER 7 Object-Oriented Programming,Part 2: User-Defined Classes
7.6 Writing Mutator Methods
As we have discussed, we declare the instance variables as private to encapsu-
late the data of the class: We allow only the class methods to directly set the
values of the instance variables. Thus, it is customary to provide a public ...