Mutator Methods in AP Computer Science A
In AP Computer Science A, a mutator method is a method written in a class that changes the value of one or more of an object's instance variables. It is usually a void method (a setter like setName), so its job is to update the object's state rather than return a value.
What is Mutator Methods?
A mutator method is a method that changes an object's state. In practice, that means it assigns a new value to at least one instance variable. The classic example is a setter like public void setBalance(double newBalance) that runs balance = newBalance;. After you call it, the object is different than it was before.
Most mutators are void methods. Per EK 3.5.A.1, a void method has the keyword void in its header and does not return a value. That fits the mutator's purpose. You call it to do something to the object, not to get something back. Compare that to an accessor (a getter), which is non-void and hands back a value (EK 3.5.A.2). Think of it this way. Accessors read the object, and mutators write to it.
Why Mutator Methods matters in AP® Computer Science A
Mutator methods live in Unit 3: Class Creation, specifically Topic 3.5 Writing Methods. They directly support learning objective AP Comp Sci A 3.5.A, which asks you to develop code that defines an object's behaviors through methods and to determine the result of calling those methods. To "determine the result" of a mutator call, you track how an instance variable changes, because there's no return value to read. Mutators also matter because of encapsulation. Instance variables are kept private, so a mutator is the controlled, public doorway for changing them. When you write a class on the exam, you'll be expected to know which methods should change state and which shouldn't.
Keep studying AP® Computer Science A Unit 3
How Mutator Methods connects across the course
Accessor Methods (Unit 3)
Accessors and mutators are two halves of the same design. An accessor like getBalance() returns an instance variable's value without changing it. A mutator like setBalance() changes the value and usually returns nothing. If a method's job is to report, it's an accessor. If its job is to update, it's a mutator.
Encapsulation and Instance Variables (Unit 3)
Because instance variables are private, code outside the class can't touch them directly. Mutators are the official way in. That lets the class control changes, for example refusing a negative balance inside the method before it updates the variable.
Return Type and Void Methods (Unit 3)
EK 3.5.A.1 and 3.5.A.2 split methods into void and non-void, and that split lines up neatly with mutators and accessors. Reading a method header is often enough to guess its role. A void return type is a strong hint you're looking at a mutator.
Side Effects (Unit 3)
Changing an instance variable is a side effect, meaning the method leaves a lasting change on the object beyond anything it returns. For a mutator, that side effect is the whole point. Tracing code means remembering that after a mutator runs, later calls see the new value.
Is Mutator Methods on the AP® Computer Science A exam?
On the multiple-choice section, mutator methods mostly show up as classification and tracing questions. Typical stems ask "Which of the following is an example of a mutator method?" or "Which statement about mutator methods is true?" You'll need to spot the method that changes an instance variable, usually a void method that assigns a parameter to a field. Questions also pair it with accessors. A method that "calculates and sends back a student's GPA" returns a value and changes nothing, so it's an accessor, not a mutator. Tracing questions call a mutator and then an accessor, and you have to figure out the updated value. No released FRQ has used the term "mutator" verbatim, but the class-design FRQ regularly asks you to write methods that update an object's instance variables. That's mutator logic even when the word isn't printed.
Mutator Methods vs Accessor Methods
An accessor returns information about an object without changing it. It has a non-void return type, like public double getBalance(). A mutator changes an instance variable and is usually void, like public void setBalance(double b). Ask yourself one question. Does this method alter the object's state? If yes, it's a mutator. If it only hands back a value, it's an accessor.
Key things to remember about Mutator Methods
A mutator method changes the value of at least one of an object's instance variables.
Most mutator methods are void, meaning their header uses the keyword void and they return no value (EK 3.5.A.1).
Accessor methods return a value without changing the object, while mutator methods change the object and usually return nothing.
Mutators support encapsulation by giving outside code a controlled way to update private instance variables.
When you trace code, a mutator call changes the object's state, so any later accessor call will reflect the new value.
Frequently asked questions about Mutator Methods
What is a mutator method in AP Computer Science A?
A mutator method is a method in a class that changes one or more of an object's instance variables, like setName or setBalance. It's usually a void method, and it falls under Topic 3.5 Writing Methods in Unit 3.
Do mutator methods always have to be void?
No, but they usually are. What makes a method a mutator is that it changes an instance variable, not its return type. Most AP examples are void setters, though, so a void header is a strong clue.
What is the difference between an accessor and a mutator method?
An accessor returns an object's data without changing it, such as getBalance() returning a double. A mutator changes the object's data, such as setBalance(double b) assigning a new value to balance. One reads and the other writes.
Is a method that calculates and returns a GPA a mutator?
No. If it calculates a value and sends it back without changing any instance variables, it's acting as an accessor. It would only count as a mutator if it also updated the object's stored data.
Why use a mutator instead of changing the instance variable directly?
Instance variables are declared private for encapsulation, so outside code can't access them directly. A mutator gives controlled access, and it can check the new value (for example, rejecting a negative number) before updating the field.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.