AP® Computer Science A glossary
Mutator method
Also called: setter, modifier method
Course: AP® Computer Science A Shows up in: Unit 3: Class Creation
A method that changes the values of instance variables or class variables; it is often a void method.
Appears in the course and exam description (CED): 3.5.A.6
Because instance variables are private, mutators are the only way other classes can change an object’s state. A good mutator can check its argument and refuse values that make no sense.
public class Bottle
{
private int amount;
public void drink(int ml) // mutator
{
if (ml > 0 && ml <= amount)
{
amount -= ml;
}
}
} Learn it properly, with a checkpoint 3.5 Methods: How to Write Them