---
title: "AP CSA 3.5: Writing Methods in Java Explained"
description: "Learn how void and non-void methods work in AP Computer Science A, including return by value, accessor and mutator methods, and primitive parameter passing."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS"
type: "study-guide"
subject: "AP Computer Science A"
unit: "Unit 3 – Class Creation"
lastUpdated: "2026-06-08"
---

# AP CSA 3.5: Writing Methods in Java Explained

## Summary

Learn how void and non-void methods work in AP Computer Science A, including return by value, accessor and mutator methods, and primitive parameter passing.

## Guide

## TLDR
Writing methods in [AP Computer Science A](/ap-comp-sci-a "fv-autolink") means defining the [behaviors](/ap-comp-sci-a/key-terms/behavior "fv-autolink") 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](/ap-comp-sci-a/key-terms/compile "fv-autolink") 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](/ap-comp-sci-a/key-terms/mutator-methods "fv-autolink") 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](/ap-comp-sci-a/key-terms/tracing "fv-autolink") what a [method call](/ap-comp-sci-a/key-terms/method-call "fv-autolink") 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](/ap-comp-sci-a/unit-1/objects-instances-of-classes/study-guide/EcpFHGcIKu6385hMohLe "fv-autolink") or class variable; mutator methods change [variable](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") 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](/ap-comp-sci-a/key-terms/loop "fv-autolink") 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](/ap-comp-sci-a/key-terms/expression "fv-autolink") 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](/ap-comp-sci-a/unit-1/creating-and-storing-objects/study-guide/rUOTKl6Ih5noXJ0GtxJF "fv-autolink") 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](/ap-comp-sci-a/unit-2/algorithms-with-selection-and-repetition/study-guide/42crNSZyW8IRsntk9IHe "fv-autolink") (`if`) or [iteration](/ap-comp-sci-a/unit-2/while-loops/study-guide/7qGsGOh1UKALAWpJhZOi "fv-autolink") (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](/ap-comp-sci-a/key-terms/object "fv-autolink"), 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](/ap-comp-sci-a/unit-4/recursion/study-guide/p4D3YegZCLwQ3KJVvsd4 "fv-autolink") 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](/ap-comp-sci-a/key-terms/primitives "fv-autolink") 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](/ap-comp-sci-a/key-terms/method-header "fv-autolink") 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](/ap-comp-sci-a/unit-2/for-loops/study-guide/DJuLxKz6SiSAX2cEVmCt "fv-autolink") 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](/ap-comp-sci-a/key-terms/null "fv-autolink")." 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](/ap-comp-sci-a/key-terms/return-value "fv-autolink"). 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.

## Related AP Computer Science A Guides

- [3.8 Scope and Access](/ap-comp-sci-a/unit-3/scope-and-access/study-guide/56FUK4RSofr7slzwm6xm)
- [3.3 Anatomy of a Class](/ap-comp-sci-a/unit-3/anatomy-of-a-class/study-guide/DcGY5KOyK98H9Fn2w8jh)
- [3.9 This Keyword](/ap-comp-sci-a/unit-3/this-keyword/study-guide/Zste3M7m756uzwR0zCQK)
- [3.7 Static Variables and Methods](/ap-comp-sci-a/unit-3/static-variables-and-methods/study-guide/zzhHVbXBRCZQ7ng3EeWX)
- [3.4 Constructors](/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk)
- [3.1 Abstraction and Program Design](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz)

## Vocabulary

- **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.

## FAQs

### 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.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS#what-does-writing-methods-mean-in-ap-csa","name":"What does writing methods mean in AP CSA?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS#what-is-the-difference-between-void-and-non-void-methods","name":"What is the difference between void and non-void methods?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS#what-does-return-do-in-a-java-method","name":"What does return do in a Java method?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS#what-are-accessor-and-mutator-methods","name":"What are accessor and mutator methods?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS#how-are-primitive-arguments-passed-to-methods-in-ap-csa","name":"How are primitive arguments passed to methods in AP CSA?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS#how-is-writing-methods-tested-on-ap-computer-science-a-frqs","name":"How is writing methods tested on AP Computer Science A FRQs?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}
```
