01 / 21

Java Methods
How to Write Them

Learn to define, call, and return values from methods: the fundamental building blocks of organized Java programs.

AP Computer Science A

What Is a Method?

A method is a named block of code that performs a specific task. Instead of writing the same logic over and over, you write it once inside a method and call it whenever you need it.

Why Methods Matter

Reusability
Write once, call many times. No duplicate code cluttering your program.
Organization
Break large problems into small, named pieces that are easier to read and debug.
Abstraction
Callers don't need to know how a method works: just what it does and what it returns.

Anatomy of a Method

public static int add(int a, int b) {
    // Method Body
    int sum = a + b;
    
    // Return Statement
    return sum;
}
              
Method Signature Return type + Name Logic goes here Sends value back

The Method Header Explained

1
Access modifier: public or private controls who can call the method.
2
Static keyword: static means the method belongs to the class, not an instance. Omit it for instance methods.
3
Return type: int, double, String, boolean, or void (returns nothing).
4
Method name: a verb-based camelCase identifier describing what it does.
5
Parameter list: zero or more typed variables in parentheses, separated by commas.

void vs. Return Methods

void Methods
Perform an action (print, modify) but send nothing back. No return statement needed (or use bare return;).
Return Methods
Compute a value and send it back to the caller. Must include return value; matching the declared type.

Parameters vs. Arguments

DEFINITION (Parameters)
public static int add(
  int a, int b
)
              
CALL (Arguments)
int result = add(
  3, 7
);
              
\(a \leftarrow 3\)
\(b \leftarrow 7\)
Parameters: placeholders Arguments: actual values

How to Write a Method: Step by Step

1
Decide the purpose. What single task should this method perform?
2
Choose the return type. Does it produce a value (int, String, etc.) or just act (void)?
3
Name it clearly. Use a verb: calculateArea, isEven, printGreeting.
4
List the parameters. What information does the method need from the caller?
5
Write the body. Implement the logic, then return the result (if non-void).

Example 1: A Simple void Method

public static void greet(String name) { System.out.println("Hello, " + name + "!"); }

Notice there is no return statement. The return type is void, so the method simply executes and ends.

Example 2: A Return Method

public static double circleArea(double radius) { return Math.PI * radius * radius; }

The return type double tells the compiler: this method promises to send back a decimal number. The formula \( \pi r^2 \) is computed and returned.

Example 3: Boolean Return

public static boolean isInRange(int value, int low, int high) { return value >= low && value <= high; }

This method takes three parameters. The boolean expression is evaluated and its result (true or false) is returned directly: no if-statement needed.

Calling Methods & Using Return Values

// Store in a variable int total = add(3, 7); // Use directly in an expression System.out.println(add(3, 7) * 2); // Pass as an argument to another method double area = circleArea(add(3, 7));

Method Overloading

Java allows multiple methods with the same name as long as their parameter lists differ in number or type. This is called overloading.

public static int max(int a, int b) { return (a > b) ? a : b; } public static int max(int a, int b, int c) { return max(max(a, b), c); }

Scope & Local Variables

Class scope methodA()
int x = 5;
String s = "hi";
              
if block
int y = 10;
// y only exists here
              
methodB()
int x = 99;
// Different x!

// Cannot see
// methodA's variables

✕ s is not visible
              

Common Mistakes

✕ Missing Return
Declaring a return type but forgetting the return statement on some paths. The compiler will error.
✕ Wrong Argument Types
Passing a String where an int is expected. Argument types must match parameter types (or be auto-convertible).
✕ Ignoring Return Values
Calling a method like add(3, 7); without storing or using the result. The value is computed then discarded.

Method Quick Reference

Feature void Method Return Method
Return type void int, double, String, boolean, etc.
Return statement Optional (bare return;) Required on all paths
Can store result? No Yes
Example use Printing, modifying state Computing, validating, transforming
Call syntax greet("Hi"); int x = add(3, 7);

Question 1 of 5

What is the return type of a method that does not return a value?

Question 2 of 5

What is the value of mystery(3, 4)?

Question 3 of 5

Which of the following is the correct way to call a static method named average that takes two double parameters and returns a double?

Question 4 of 5

Two methods have the same name but different parameter lists. This is an example of:

Question 5 of 5

What is printed to the console?