All > Technology > Programming > Java
A method specifically designed to allow controlled modification of a
private
attribute of a class. By convention, we name mutators with aset
prefix followed by the name of the attribute being modified. For instance, the mutator for an attribute namedspeed
would besetSpeed
. By making an attribute private, we prevent objects of other classes from altering its value other than through its mutator. The mutator is able to check the value being used to modify the attribute and reject the modification if necessary. In addition, modification of one attribute might require others to be modified in order to keep the object in a consistent state. A mutator method can undertake this role. Mutators are used both to grant safe access to the value of a private attribute and to protect attributes from modification by objects of other classes. The latter goal is achieved by choosing an appropriate visibility for the mutator.- Browse Related Terms: Abstraction, accessor method, coupling, divide and conquer, factory pattern, floating point number, inconsistent state, information hiding, locale, mutator method, object-oriented design, pattern, singleton pattern, state, wrapper