Verified for the 2025 AP Computer Science A exam•Citation:
Accessor methods are the windows into an object's state, allowing controlled access to an object's data without exposing the underlying instance variables. In Java's object-oriented paradigm, these methods play a crucial role in maintaining encapsulation while providing a way for other objects to retrieve information about an object. This section explores how non-void methods work, how they return values, and how special accessor methods like toString()
help objects communicate their state to the outside world.
Accessor methods (commonly called "getters") are methods that:
getName()
)public class Student { private String name; private double gpa; // Constructor public Student(String name, double gpa) { this.name = name; this.gpa = gpa; } // Accessor methods public String getName() { return name; } public double getGPA() { return gpa; } }
Unlike void methods, non-void methods:
void
)A return statement consists of the return
keyword followed by an expression whose value becomes the result of the method call. This statement immediately terminates method execution and sends the specified value back to wherever the method was called.
// Void method (no return value) public void printInfo() { System.out.println("Name: " + name); System.out.println("GPA: " + gpa); // No return statement required } // Non-void method (returns a String) public String getDescription() { return "Student: " + name + " with GPA " + gpa; // Must return a String (compatible with the return type) }
A method's return type can be:
int
, double
, boolean
, etc.)void
keyword (for methods that don't return a value)public boolean isHonorRoll() { return gpa >= 3.5; // Returns a boolean value } public String[] getCourses() { return courses; // Returns an array (reference type) } public Student getBestFriend() { return bestFriend; // Returns a Student object (reference type) }
When a method returns a value, Java uses "return by value" semantics:
For primitive types, this means the actual value is copied. For reference types, the reference (not the object itself) is copied.
public int getAge() { return age; // A copy of the int value is returned } public Student[] getClassmates() { return classmates; // A copy of the array reference is returned // (not a copy of the array itself) }
When returning a reference to an object:
public class School { private ArrayList<Student> students; // CAUTION: This returns a reference to the actual list public ArrayList<Student> getStudentList() { return students; // Returns a reference to the actual list } // Better approach: Return a copy to protect encapsulation public ArrayList<Student> getStudentListSafe() { return new ArrayList<Student>(students); // Returns a copy } }
The return
keyword serves two important purposes:
public double calculateFinalGrade(double participation, double homeworkAvg, double midterm, double finalExam) { // Input validation if (participation < 0 || homeworkAvg < 0 || midterm < 0 || finalExam < 0) { return -1; // Error code - method ends here if any grade is negative } // Calculate weighted average (only executed if all grades are valid) return participation * 0.1 + homeworkAvg * 0.4 + midterm * 0.2 + finalExam * 0.3; }
The toString()
method is a special accessor method that:
public
access modifier so it can be called from any classString
representation of an objectWhen overriding methods like toString()
, it's best practice to use the @Override annotation. This annotation tells the compiler that you intend to override a method from a superclass or implement a method from an interface. If you make a mistake (like a typo in the method name or incorrect parameters), the compiler will catch the error.
public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } // Override the default toString() method @Override public String toString() { return "Rectangle [length=" + length + ", width=" + width + ", area=" + (length * width) + "]"; } }
The toString()
method is called automatically in these contexts:
Rectangle rect = new Rectangle(5.0, 3.0); // toString() is called automatically in these cases: System.out.println(rect); // Prints: Rectangle [length=5.0, width=3.0, area=15.0] System.out.println("My shape: " + rect); // Prints: My shape: Rectangle [length=5.0, width=3.0, area=15.0] String description = rect.toString(); // Explicitly calling toString()
Guideline | Example |
---|---|
Name accessors with "get" + variable name | getName() , getBalance() |
Return the same type as the instance variable | public double getBalance() for a double balance |
Keep accessors simple – focused on returning data | Avoid complex logic in accessor methods |
Consider returning copies of mutable objects | return new ArrayList<>(items) instead of return items |
Include a toString() method in every class | Override Object's default toString() |
public class BankAccount { private String accountNumber; private String ownerName; private double balance; private ArrayList<Transaction> transactions; // Constructor omitted for brevity // Simple accessor methods public String getAccountNumber() { return accountNumber; } public String getOwnerName() { return ownerName; } public double getBalance() { return balance; } // Accessor that returns a copy to protect the original public ArrayList<Transaction> getTransactionHistory() { return new ArrayList<>(transactions); } // Derived accessor (computes a value) public boolean isOverdrawn() { return balance < 0; } // toString method @Override public String toString() { return "Account #" + accountNumber + " | Owner: " + ownerName + " | Balance: $" + String.format("%.2f", balance); } }
Accessor methods provide controlled access to an object's internal state
Non-void methods must include a return
statement with a value compatible with the return type
Java uses "return by value" - primitive values are copied, while for objects, references are copied
The toString()
method provides a String representation of an object and is called automatically in string contexts
When returning mutable objects, consider returning copies to maintain encapsulation
When an object is passed to System.out.print
or println
, its toString()
method is called automatically