AP CSA Accessors Mutators

Accessor and Mutator Methods in AP CSA

Because instance variables are private, outside code can’t read or modify them directly. Accessor and mutator methods are the controlled gateway — this design principle is called encapsulation.

Accessor Methods (Getters)

Return the value of a private instance variable. Always have a return type that matches the variable. Never void.

public class Student {
    private String name;
    private int grade;

    // Accessor -- returns the value
    public String getName() {
        return name;
    }

    public int getGrade() {
        return grade;
    }
}

Mutator Methods (Setters)

Modify a private instance variable. Almost always void. Can include validation logic.

    // Mutator -- modifies the value
    public void setGrade(int newGrade) {
        if (newGrade >= 0 && newGrade <= 100) {
            grade = newGrade;   // only update if valid
        }
    }
Naming Convention: Accessors start with get. Mutators start with set. The AP exam and FRQ rubrics expect this convention. getName() not fetchName().

📝 Practice Question 1

Which of the following is a correctly written accessor method for a private double instance variable named price?

📝 Practice Question 2

A BankAccount class has a private balance field. Which of the following mutator designs BEST demonstrates encapsulation?

✅ Exam Tip: FRQ #2 almost always requires writing at least one accessor and one mutator. The accessor must have the correct return type and use return. The mutator must be void and actually assign the parameter to the instance variable.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]