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.
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.
Method-design vocabulary.
The terms used in every AP class FRQ.
- 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.
How state and methods are tested.
Encapsulation and signatures dominate.
- Fields private. Required for encapsulation credit.
- Accessors return the correct type. Match signature.
- Mutators void. Don't return.
- Use
thiswhen parameter shadows field.
Person class with derived method.
State + behavior.
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.
Method-design pitfalls.
Encapsulation breaks are most common.
- Public instance variables. Loses encapsulation credit.
- Wrong return type. Accessor's return must match the field.
- Returning from a void mutator. Compile error.
- Forgetting
thiswhen shadowed. Parameter assigns to itself.
Method types.
Accessor, mutator, behavior.
Method Reference
AP Quick Reference| Type | Returns | Body |
|---|---|---|
| Accessor | Field type | return field; |
| Mutator | void | this.field = value; |
| Behavior | Varies | Uses state; may return derived value. |
How to master object state.
Private fields; public methods; this when needed.
- 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.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 01 Object State and Method Writing FRQ Practice
AP-style topic practice assessment