6m left·0%
Reading Time: 6 min
Last Updated: March 9, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: March 9, 2026
Main Ideas: 5

Topic 3.5 Notes – Methods: How to Write Them

Verified for 2027 AP® Computer Science A Exam
Read aloud
You’re defining what an object can do and controlling how it behaves. This includes choosing the correct return type, using parameters properly, and understanding how control flows in and out of a method.

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.

Study guide illustration

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 return statement 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:

  1. Sends a value back (if non-void)
  2. 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 return in the same block will never execute.
  • A return inside a loop or if exits 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);
  • 4 and 3 are arguments
  • a and b are 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:

  1. Match the header exactly to what’s given.
  2. Use the parameters provided.
  3. 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 x into the parameter.
  • Execute the body.
  • Evaluate the return expression.
  • 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.

Key Takeaways

A void method never returns a value, and a non-void method must return exactly one value of the declared type.
The return statement immediately exits the entire method, even inside loops or conditionals.
Primitive arguments are passed by value, so the original variable never changes.
Every possible execution path in a non-void method must return a value.
Accessors return instance variables, mutators modify them.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining