How do you design a method with parameters and a return value, and how does it use the object's own data?
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.
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.6) wants you to design and implement methods that go beyond simple getters and setters: methods that take parameters, do a computation (often using the object's instance variables), and either return a result or perform an action. This is the heart of the free-response section, where you implement a specified method. You need to handle the method header, the body, the return value, and calling one method from another.
The method header
public double scale(double factor) {
// ^modifier ^return ^name ^parameter
return value * factor;
}
Parameters versus instance variables
A parameter is a local variable that receives a copy of the argument when the method is called; it exists only during that call. An instance variable belongs to the object and persists between calls. A method can use both: it reads its parameters for the inputs of this call, and reads or writes instance variables for the object's lasting state. When a parameter and an instance variable share a name, the parameter hides the field unless you use this (Topic 5.9).
Returning a value versus void
If any path through a non-void method can reach the end without returning, the code does not compile ("missing return statement"). Guard clauses must each return, and the final fall-through must return too.
Calling other methods
A method can call another method of the same object by name, without a dot:
public double areaPlusBorder() {
return area() + perimeter(); // calls this object's other methods
}
This breaks a complex method into small, testable helpers and avoids repeating logic.
Try this
Q1. Name the four parts of a method header. [1 point]
- Cue. Modifiers, return type, name, and parameter list.
Q2. Explain why a non-void method that returns a value only inside an if may fail to compile. [2 points]
- Cue. A non-void method must return a value on every path; if the
ifis false and noreturnfollows, a path reaches the end without returning, so the compiler reports a missing return statement.
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. Consider this class.
```java
public class Counter {
private int value;
public Counter(int v) { value = v; }
public int addAndGet(int n) {
value = value + n;
return value;
}
}
Counter c = new Counter(10);
int result = c.addAndGet(5);
System.out.println(result + " " + c.addAndGet(2));
```
What is printed?
(A) `15 17`
(B) `15 15`
(C) `10 12`
(D) `17 17`
(E) `5 2`
Show worked answer →
The answer is (A).
addAndGet adds its parameter to the instance variable value and returns the new value. c starts with value = 10. The first call addAndGet(5) makes value = 15 and returns 15, so result is 15. The second call addAndGet(2) makes value = 17 and returns 17. So the print is 15 17. (B) forgets the second call also changes value; (C) ignores the mutation; (E) returns the parameters.
Markers reward tracing that the method both changes the instance variable and returns the updated value, across two calls.
AP 2020 (style)4 marksFree response (code writing). A class `Grades` has a private `int` instance variable `total` and a private `int` instance variable `count`. Write a method `average` that returns the average mark as a `double` (total divided by count), and returns 0.0 when `count` is 0 to avoid dividing by zero.
Show worked answer →
A 4-point question testing a method using instance variables with a guard.
public double average() {
if (count == 0) {
return 0.0;
}
return (double) total / count;
}
Point 1: the method has return type double. Point 2: the guard if (count == 0) return 0.0; prevents division by zero. Point 3: the computation uses the instance variables total and count. Point 4: casting total to double so the division is real, not integer division. Omitting the cast would truncate the average; omitting the guard risks an ArithmeticException.
Related dot points
- 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.
- 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.3 Documentation with Comments: write comments that document a method, including its precondition and postcondition, to describe what the method assumes and guarantees.
A focused answer to AP CSA Topic 5.3, covering line and block comments, what makes a useful comment, preconditions and postconditions, why they form a contract for a method, and how they guide both callers and implementers, with a fully worked example.
- Topic 5.9 this Keyword: use the implicit parameter this to refer to the current object, disambiguate an instance variable from a parameter of the same name, and pass the current object to another method.
A focused answer to AP CSA Topic 5.9, covering what this refers to, using this to distinguish an instance variable from a shadowing parameter, calling another method on the current object with this, and passing this as an argument, 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)