Topic 3.5 Notes – Methods: How to Write Them
1. What a Method Is and How It Defines Behavior
A method is a block of code inside a class that defines a behavior. When a method is called, program control jumps to that method, runs its code, then returns to where it was called.
The diagram below shows that jump-and-return process. Each horizontal arrow represents a method call, and the thin vertical bars show the time a method is actively running before control returns to the caller.

Method calls and returns in a sequence diagram
Here’s the general structure:
public returnType methodName(parameterType parameterName) {
// body
}
Every method has two main parts:
- Header (signature)
- Includes the return type, method name, and parameters
- Body
- The statements that actually do the work
The return type is part of the method’s contract. It tells you what kind of value (if any) comes back.
Void Methods
- Use the keyword
void - Do not return a value
- Often change instance variables (mutators)
public void setLevel(int newLevel) {
level = newLevel;
}
Non-void Methods
- Return exactly one value
- Must include a
returnstatement with a compatible expression
public int getLevel() {
return level;
}
If the return type is int, the expression after return must evaluate to an int. That’s non-negotiable on the AP exam.
2. Return Statements and Flow of Control
The return keyword does two things:
- Sends a value back (if non-void)
- Immediately exits the method
That second part trips people up.
public boolean isEven(int num) {
if (num % 2 == 0) {
return true;
}
return false;
}
Once return true; runs, the method is over. Nothing after it in that execution path runs.
Important rules:
- Code after a guaranteed
returnin the same block will never execute. - A
returninside a loop orifexits the entire method, not just the loop.
On quizzes, you’ll often see a non-void method missing a return in one branch. Ask yourself: Does every possible path return something? If not, it won’t compile.
3. Parameters and Argument Passing
Methods can take input using parameters.
public int multiply(int a, int b) {
return a * b;
}
Call:
int result = multiply(4, 3);
4and3are argumentsaandbare parameters
Primitives Are Passed by Value
When you pass a primitive (int, double, boolean):
- The method gets a copy
- Changing the parameter does not change the original variable
public void changeValue(int x) {
x = 100;
}
If you call this with int score = 5;, score is still 5 afterward.
The diagram below shows what is happening in memory when the method is called.

This exact idea shows up in multiple choice questions all the time.
4. Accessor and Mutator Methods
These control access to instance variables.
Accessor Methods
- Return the value of an instance variable
- Always non-void
- Do not change the object’s state
public int getHealth() {
return health;
}
They give a copy of the value, not direct access to the variable itself.
Mutator Methods
- Change instance variables
- Often
void
public void setHealth(int newHealth) {
health = newHealth;
}
If a method modifies an instance variable, it’s a mutator. If it returns one, it’s an accessor. Expect to identify or write both.
5. Writing and Tracing Methods on the Exam
When you’re asked to write a method:
- Match the header exactly to what’s given.
- Use the parameters provided.
- Make sure every path returns (if non-void).
When tracing a method call:
int x = 6;
int y = doubleValue(x);
Trace it like this:
- Copy
xinto the parameter. - Execute the body.
- Evaluate the
returnexpression. - Assign the returned value to
y.
Common mistakes I see:
- Printing instead of returning.
- Returning the wrong type.
- Trying to return multiple values.
- Changing a primitive parameter and expecting the original to change.