How does a mutator method change an object's state, and how can it validate the new value?
Topic 5.5 Mutator Methods: write mutator (setter) methods, usually void, that change an object's instance variables, including methods that validate or constrain the new value before assigning it.
A focused answer to AP CSA Topic 5.5, covering mutator (setter) methods, the void return type, changing instance variables from a parameter, validating a new value before assigning, and why mutators protect encapsulated data, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 5.5) wants you to write mutator methods (also called setters): methods that change an object's instance variables. A mutator is usually void and takes a parameter holding the new value. The important design idea is validation: because the data is private, the mutator is the gatekeeper, so it can check a new value before assigning it and reject anything invalid - the payoff of encapsulation.
What a mutator does
public class Student {
private int grade;
public void setGrade(int g) { // mutator: changes grade, returns nothing
grade = g;
}
}
Direction of assignment matters
A frequent bug is writing the assignment backwards. grade = g; copies the parameter g into the instance variable grade (correct). g = grade; copies the instance variable into the local parameter and then throws it away when the method ends (wrong - the object is unchanged). Always put the instance variable on the left.
Validation: the point of a setter
public void setAge(int a) {
if (a >= 0) { // reject negative ages
age = a;
}
}
Here a negative argument is simply ignored, leaving age unchanged. Other designs might clamp the value to a range or substitute a default. Validation is the concrete benefit of encapsulation: outside code cannot bypass the check by writing the field directly.
Try this
Q1. What return type does a typical mutator method have? [1 point]
- Cue.
void- it changes the object's state rather than returning a value.
Q2. Explain why a mutator is a good place to validate a new value, and what makes that validation effective. [2 points]
- Cue. Because instance variables are
private, the mutator is the only way to change them, so a check inside it cannot be bypassed; it can reject or adjust invalid values before assigning, keeping the object's state valid.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2021 (style)1 marksMultiple choice. A class `Clock` has a private `int` instance variable `hour`. Which is a correct mutator that sets the hour only when the argument is a valid hour from 0 to 23?
(A) `public void setHour(int h) { if (h >= 0 && h <= 23) hour = h; }`
(B) `public int setHour(int h) { hour = h; }`
(C) `public void setHour(int h) { return h; }`
(D) `public void setHour() { hour = 0; }`
(E) `public void setHour(int h) { h = hour; }`
Show worked answer →
The answer is (A).
A mutator is typically void and changes an instance variable from its parameter. (A) validates the argument with h >= 0 && h <= 23 and only then assigns it to hour, protecting the object from invalid data. (B) declares a return type but does not return - it does not compile. (C) tries to return from a void method. (D) ignores the parameter. (E) assigns backwards (h = hour), changing only the local parameter, not the instance variable.
Markers reward a void mutator that validates the argument before assigning it to the instance variable.
AP 2020 (style)4 marksFree response (code writing). A class `Account` has a private `double` instance variable `balance`. Write a mutator method `deposit` that takes a `double` parameter `amount`, and adds it to `balance` only when `amount` is positive; otherwise it leaves `balance` unchanged. The method returns nothing.
Show worked answer →
A 4-point question testing a validating mutator.
public void deposit(double amount) {
if (amount > 0) {
balance = balance + amount;
}
}
Point 1: the method is void and takes the double parameter. Point 2: it validates with amount > 0. Point 3: only when valid does it update balance by adding the amount. Point 4: when the amount is not positive, the if is skipped and balance is unchanged. Replacing balance + amount with amount, or omitting the validation, would lose marks.
Related dot points
- Topic 5.4 Accessor Methods: write accessor (getter) methods, including a toString method, that return information about an object's state without changing it.
A focused answer to AP CSA Topic 5.4, covering accessor (getter) methods, the non-void return type, why accessors do not change state, the toString method and how println uses it, and returning computed values, with a fully worked example.
- Topic 5.6 Writing Methods: design and implement methods that take parameters, perform a computation possibly using the object's instance variables, and return a result or perform an action.
A focused answer to AP CSA Topic 5.6, covering the method header (modifiers, return type, name, parameters), how a method uses instance variables and parameters, returning a value versus void, calling one method from another, and tracing a method, with a fully worked example.
- Topic 5.1 Anatomy of a Class: identify the parts of a class definition (the class header, private instance variables, constructors and methods) and explain encapsulation through access modifiers.
A focused answer to AP CSA Topic 5.1, covering the class header, private instance variables, constructors and methods, the meaning of public and private, encapsulation, and how the pieces combine into a working class, with a fully worked example.
- Topic 5.2 Constructors: write constructors that initialise an object's instance variables from parameters, including overloaded and default constructors, and explain the default initial values.
A focused answer to AP CSA Topic 5.2, covering constructor syntax, initialising instance variables from parameters, the no-argument constructor, overloading, default values when a variable is not set, and the difference between a parameter and an instance variable, with a fully worked example.
- Topic 5.8 Scope and Access: distinguish local variable scope from instance variable scope, and use the access modifiers public and private to control where a class member can be used.
A focused answer to AP CSA Topic 5.8, covering the scope of local variables and parameters versus instance variables, variable shadowing, the public and private access modifiers, and how access control supports encapsulation, with a fully worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)