Fiveable

💻AP Computer Science A Unit 3 Review

QR code for AP Computer Science A practice questions

3.5 Writing Methods

3.5 Writing Methods

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

TLDR

Writing methods in AP Computer Science A means defining the behaviors of a class. A void method runs code but returns nothing, while a non-void method evaluates a return expression and hands back a single value of its return type. When you pass a primitive value into a method, the parameter gets a copy, so changing the parameter never affects the original argument.

Why This Matters for the AP Computer Science A Exam

Writing methods shows up in both the multiple-choice and free-response sections. You will be asked to determine what a method returns, explain why code does not compile or behave as intended, and write methods that match a given specification. In free-response class design, you build methods inside a class, including accessor and mutator methods that work with private instance variables, so being comfortable writing clear method headers and return logic directly supports that work.

The big skills here are tracing what a method call produces, choosing between void and non-void correctly, and predicting how parameter values behave once a method runs.

Key Takeaways

  • A void method does not return a value; a non-void method returns exactly one value that matches its return type.
  • The return keyword sends control back to the caller, and any code after a reached return never runs.
  • Accessor methods are non-void and hand back a copy of an instance or class variable; mutator methods change variable values and are often void.
  • When you pass a primitive argument, the parameter is a copy, so changes inside the method do not affect the original.
  • A return inside a loop or if statement stops that statement and exits the whole method right away.

Void vs Non-Void Methods

Every method is either void or non-void, and the header tells you which.

A void method does not return a value. The keyword void appears before the method name in the header.

</>Java
public void setScore(int newScore) {
    score = newScore;
}

A non-void method returns a single value. Instead of void, the header lists the return type, and the method must evaluate a return expression that is compatible with that type.

</>Java
public int getScore() {
    return score;
}

In non-void methods, the return expression is evaluated and its value is handed back to the caller. This is called return by value: the caller receives the value, not the expression itself.

How the return Keyword Controls Flow

The return keyword sends the flow of control back to the point where the method was called. Two rules matter most:

  • Any code that comes sequentially after a return will never run.
  • A return placed inside a selection (if) or iteration (loop) statement halts that statement and exits the method immediately.
</>Java
public boolean contains(int[] nums, int target) {
    for (int n : nums) {
        if (n == target) {
            return true;   // exits the method right here
        }
    }
    return false;          // only reached if target never found
}

Once return true; runs, the loop stops and the method ends. The final return false; only executes if the loop finishes without finding the target.

Accessor and Mutator Methods

Methods are how you define the behaviors of an object, and two common kinds work directly with a class's variables.

An accessor method (often called a getter) lets objects of other classes obtain a copy of the value of an instance variable or class variable. An accessor is always a non-void method because it returns that value.

</>Java
public String getName() {
    return name;
}

A mutator method (also called a modifier or setter) changes the values of instance variables or class variables. A mutator is often a void method because it updates state rather than returning a value.

</>Java
public void setName(String newName) {
    name = newName;
}

Accessor and mutator methods are how you let outside code interact with private data safely, which keeps the class in control of its own state.

Parameters and Passing Primitives

Methods with parameters receive values through those parameters and use them to do their work. Parameters let you generalize a method so it works with many different inputs.

The key rule for this topic is how primitives are passed. When an argument is a primitive value (like an int, double, boolean, or char), the parameter is initialized with a copy of that value. Changes to the parameter inside the method have no effect on the original argument.

</>Java
public class Demo {
    public static void addTen(int x) {
        x = x + 10;   // changes only the local copy
    }

    public static void main(String[] args) {
        int num = 5;
        addTen(num);
        System.out.println(num);  // prints 5, not 15
    }
}

Even though addTen changes x to 15, the original num stays 5. The parameter x was a separate copy the whole time. This copy behavior is sometimes called pass-by-value for primitives.

Remember the difference between an argument and a parameter: the argument is the actual value you pass in the call (num), and the parameter is the variable in the method header that receives a copy (x).

How to Use This on the AP Computer Science A Exam

Code Tracing

When a question asks what a method returns or prints:

  • Check the header first. If it says void, the method returns nothing, so it cannot be used as a value.
  • For non-void methods, find the return expression that actually gets reached and evaluate it.
  • Stop tracing as soon as a return runs. Code after a reached return does not execute.
  • For primitive parameters, remember the method works on a copy. The original variable in the caller does not change.

Free Response

When you write methods for a class design question:

  • Match the return type in the header to what the specification says the method should produce. Use void only when nothing is returned.
  • Write accessor methods as non-void methods that return the requested variable.
  • Write mutator methods that update the instance variable, usually as void methods.
  • Make sure every path through a non-void method reaches a return of the correct type.

Common Trap

A common error is writing a non-void method that does not return a value on every possible path, or putting code after a return and expecting it to run. Trace each branch to confirm a value comes back.

Common Misconceptions

  • A void method does not "return null." It returns nothing at all, so you cannot use its call as a value in an expression.
  • Calling a mutator does not require capturing a return value. Many mutators are void and simply change the object's state.
  • Changing a primitive parameter inside a method does not change the caller's variable. The parameter is a copy.
  • A return statement does not just set a value and keep going. It immediately exits the method, skipping any remaining code.
  • A non-void method must return a value on every path that completes, not only in some branches. A return type in the header is a promise the method must keep.
  • return is not the same as printing. Returning a value hands it back to the caller; printing only displays text and does not produce a usable value.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

accessor method

A non-void method that allows objects of other classes to obtain a copy of the value of instance variables or class variables.

class variable

Variables that belong to the class itself rather than individual objects and can be accessed or modified by accessor and mutator methods.

instance variable

A variable that belongs to an object and can be accessed throughout the class, as opposed to a local variable that is limited to a specific block of code.

mutator method

A method that changes the values of instance variables or class variables, often implemented as a void method.

non-void method

A method that returns a value of a specified type that can be stored in a variable or used as part of an expression.

parameters

Variables that allow procedures to be generalized and reused with a range of input values or arguments.

primitive values

Basic data types in Java such as int, double, and boolean that store actual values directly.

return by value

The process in which a non-void method evaluates a return expression compatible with the return type and returns that value.

return statement

A statement that terminates method execution and returns control flow to the point immediately following where the method was called.

return type

The data type of the value that a non-void method returns, specified in the method header.

void method

A method that does not return a value and cannot be used as part of an expression.

Frequently Asked Questions

What does writing methods mean in AP CSA?

Writing methods means defining behaviors inside a class. A method has a header, may receive parameters, runs statements, and either returns a value or, if it is void, returns no value.

What is the difference between void and non-void methods?

A void method performs an action and returns no value. A non-void method must return exactly one value compatible with its return type, such as int, boolean, double, or String.

What does return do in a Java method?

The return keyword sends control back to the point where the method was called. In a non-void method, it also returns a value; any code after a reached return statement does not execute.

What are accessor and mutator methods?

An accessor method returns a copy of an instance or class variable and is non-void. A mutator method changes the value of an instance or class variable and is often void.

How are primitive arguments passed to methods in AP CSA?

When a primitive value is passed to a method, the parameter receives a copy of that value. Changing the parameter inside the method does not change the original variable in the caller.

How is writing methods tested on AP Computer Science A FRQs?

AP CSA FRQs often ask you to write methods that match a specification. Check the return type, use parameters correctly, make every non-void path return a compatible value, and avoid expecting primitive parameter changes to affect the caller.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot