How does an accessor method return an object's data, and what is the toString method?
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.
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.4) wants you to write accessor methods (also called getters): methods that return information about an object's state without changing it. The headline accessor is toString, which returns a String representation of the object and is called automatically when you print the object. Accessors are non-void methods, so the return type and return statement are central.
What an accessor does
public class Student {
private String name;
private int grade;
public String getName() { // accessor: returns the name, changes nothing
return name;
}
public int getGrade() {
return grade;
}
}
Accessors do not change state
By convention and by design, an accessor only reads the object's data. If a method changes an instance variable, it is a mutator (Topic 5.5), not an accessor. Keeping accessors read-only makes a class easier to reason about: calling getName() can never alter the object.
Computed accessors
An accessor need not return a stored variable directly; it can return a value computed from the state:
public double getAverage() {
return total / count; // derived from two instance variables
}
This still counts as an accessor because it reports information without changing the object.
The toString method
Because toString is called automatically, writing a good one makes objects easy to print and debug. It must return the String, never print it - printing inside toString is a common mistake.
Try this
Q1. What return type and behavior does the toString method have? [1 point]
- Cue. It is
public String toString(), returning (not printing) a String that represents the object; Java calls it automatically when the object is printed or concatenated.
Q2. Explain why a method that changes an instance variable is not an accessor. [2 points]
- Cue. An accessor only reads and returns information without altering state; a method that changes an instance variable is a mutator, because it modifies the object rather than merely reporting on it.
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 2022 (style)1 marksMultiple choice. A class `Temperature` has a private `double` instance variable `celsius`. Which method is a correct accessor that returns the temperature in Fahrenheit?
(A) `public double getFahrenheit() { return celsius * 9.0 / 5.0 + 32; }`
(B) `public void getFahrenheit() { return celsius * 9.0 / 5.0 + 32; }`
(C) `public double getFahrenheit() { celsius = celsius * 9.0 / 5.0 + 32; }`
(D) `public double getFahrenheit(double f) { return f; }`
(E) `public celsius getFahrenheit() { return celsius; }`
Show worked answer →
The answer is (A).
An accessor returns a value, so it must have a non-void return type (double here) and a return statement of the matching type. (A) computes and returns the Fahrenheit value without changing the instance variable. (B) is invalid: a void method cannot return a value. (C) is a mutator in disguise - it changes celsius and returns nothing, so it does not compile. (D) ignores the object's state. (E) uses an invalid return type.
Markers reward a non-void return type, a matching return statement, and leaving the object's state unchanged.
AP 2020 (style)4 marksFree response (code writing). A class `Rectangle` has private `int` instance variables `width` and `height`. Write two accessor methods: `getArea`, returning the area as an `int`, and `toString`, returning a String of the form "wxh" (for example "4x7" for width 4 and height 7).
Show worked answer →
A 4-point question testing accessors and toString.
public int getArea() {
return width * height;
}
public String toString() {
return width + "x" + height;
}
Point 1: getArea has return type int and returns the product. Point 2: it does not change any instance variable. Point 3: toString returns a String. Point 4: the String is built by concatenation in the exact "wxh" format. Returning the wrong type, or modifying width/height, would lose marks; toString must return, not print.
Related dot points
- 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.
- 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 2.5 Calling a Non-void Method: call a method that returns a value, and use the returned value by storing it, printing it, or including it in an expression.
A focused answer to AP CSA Topic 2.5, covering methods that return a value, the return type, using a returned value in an assignment, expression or print, the difference from a void method, and chaining method calls, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)