Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

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 an accessor does
  3. Accessors do not change state
  4. Computed accessors
  5. The toString method
  6. Try this

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

Sources & how we know this