Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. What a mutator does
  3. Direction of assignment matters
  4. Validation: the point of a setter
  5. Try this

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

Sources & how we know this