01
Overview

Methods that read and modify object state.

Every class designs around two activities: looking at its own data and changing it. Accessors and mutators are the foundation.

An object's state is the collection of its instance variables. Methods that read state are accessors (often named get...); methods that change state are mutators (often set...). Behavior methods may also use state to compute something or trigger an action.

The discipline: instance variables are private; all access goes through methods. This enforces encapsulation and lets the class control how its data is used.

On the AP exam, accessor and mutator design is the foundation of every class FRQ.

02
Core Concept

Private state; public methods.

Accessors return; mutators change.

Standard pattern

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() { return name; }      // accessor
    public int getAge() { return age; }            // accessor

    public void setName(String name) { this.name = name; }   // mutator
    public void setAge(int age) { this.age = age; }
}

Behavior method using state

public boolean isAdult() {
    return age >= 18;
}

Uses the instance variable directly to compute a derived answer.

The this keyword

When a parameter has the same name as an instance variable, this.name means "the field" and bare name means "the parameter". this.name = name stores the parameter into the field.

03
Key Vocabulary

Method-design vocabulary.

The terms used in every AP class FRQ.

Vocabulary
  • Instance variable — field stored per object.
  • Accessor (getter) — returns the value of a field.
  • Mutator (setter) — changes the value of a field.
  • Behavior method — uses state to do something or compute a derived value.
  • this — refers to the current object's fields.
  • Encapsulation — private fields with public methods.
04
AP Exam Focus

How state and methods are tested.

Encapsulation and signatures dominate.

Exam Focus
  • Fields private. Required for encapsulation credit.
  • Accessors return the correct type. Match signature.
  • Mutators void. Don't return.
  • Use this when parameter shadows field.
05
Worked Example

Person class with derived method.

State + behavior.

Worked Example AP-style reasoning

Setup. Person above; create p = new Person("Alex", 25).

  • p.getName() → "Alex".
  • p.getAge() → 25.
  • p.isAdult() → true (25 >= 18).
  • p.setAge(15); p.isAdult() → false.

Why this matters. The class controls access. A caller can't write p.age = -5 directly because age is private. To restrict input, the mutator can add validation.

06
Common Mistakes

Method-design pitfalls.

Encapsulation breaks are most common.

Watch Out
  • Public instance variables. Loses encapsulation credit.
  • Wrong return type. Accessor's return must match the field.
  • Returning from a void mutator. Compile error.
  • Forgetting this when shadowed. Parameter assigns to itself.
07
Reference Table

Method types.

Accessor, mutator, behavior.

Method Reference

AP Quick Reference
TypeReturnsBody
AccessorField typereturn field;
Mutatorvoidthis.field = value;
BehaviorVariesUses state; may return derived value.
08
Practice Tip

How to master object state.

Private fields; public methods; this when needed.

Practice Strategy
  • MCQ Practice — verify fields are private and methods have correct signatures.
  • Java Lab — build Person, Book, Car classes. Write accessor + mutator pairs.
  • FRQ Practice — start each class with private fields, then constructor, then accessors and mutators.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 16 marks Pending

AP CSA Unit 13 Topic 01 Object State and Method Writing FRQ Practice

AP-style topic practice assessment

Start