TOPIC 3.5 · STUDY
Methods: How to Write Them
A void method performs a task and does not return a value. A non-void method returns a value of its declared type and can be used in an expression. Accessors report information; mutators change the object's state. return leaves the method immediately. Primitive parameters are copies, so changing the parameter does not change the caller's variable.
3.5.A
Writing void and non-void methods
When you write a method, the return type in the header decides what the caller can do with the call. A void method runs its body to perform a task and does not send a value back. A non-void method declares a type such as int or boolean, and every path through the method ends by returning a value of that type. The caller can store that result, print it, or use it inside a larger expression.
Two common roles use that distinction. An accessor is a non-void method that returns information about the object without changing its fields. A mutator updates the object's state; mutators are usually void because their job is the change itself, not a returned answer. In the Pond class below, rain is a void mutator and getDepth is a non-void accessor.
public class Pond {
private int depth;
public Pond() {
depth = 6;
}
public void rain(int inches) {
depth += inches;
}
public int getDepth() {
return depth;
}
}
Pond lake = new Pond();
lake.rain(3);
int now = lake.getDepth();
System.out.print(now);
rain adds 3 to depth and returns nothing, so you call it as a statement. getDepth returns a copy of depth, so now stores 9 and the print shows 9. You write int now = lake.getDepth(); because getDepth is non-void. You do not write int x = lake.rain(3); because rain is void.
A void method updates state and gives nothing back. A non-void method returns a value the caller can use.
A return statement ends the method immediately. Any later statements in that method are skipped on that call. In a non-void method, return value; both exits and supplies the result. In a void method, a bare return; can exit early when no further work should run. Early return inside an if or a loop is common: once the return runs, the rest of that method body does not.
Primitive parameters are passed by value. The method receives a copy of the argument. Assigning to the parameter or updating it with operators only changes that copy. The caller's variable stays the same unless the method returns a new value and the caller stores it. To change an object's fields, write a mutator that updates those fields; do not expect changing a primitive parameter alone to rewrite the caller's variable.
Key points
- void methods perform a task and do not return a value.
- Non-void methods return a value of the declared type for use in expressions.
- Accessors report information; mutators change the object's state.
- return leaves the method immediately; later lines in that method do not run.
- Primitive parameters are copies; changing them does not change the caller's variable.
void methods are not used as if they returned a value. Do not assign from a void call or pass a void call where an expression is required.