Basic

Methods and Functions

Define reusable methods with parameters and return values.

What you will see

12 5.0

About 2 min of reading

Try It

Reusable methods take parameters and return a value. Call one, make sure every path returns, and give a method two signatures through overloading.

1

Define a static method that takes two ints and returns their sum. Call it from main and print the result, the arguments flow in and the return value comes back.

class Main { static int add(int a, int b) { return a + b; } public static void main(String[] args) { int result = add(3, 4); System.out.println(result); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
7
public static lets you call the method without first creating an object. Static helpers are the usual choice for pure functions like add.
2

A method with a return type must hit a return on every path, including the fall-through at the end. Classify a few numbers and see all three labels print.

class Main { static String classify(int n) { if (n > 0) return "positive"; if (n < 0) return "negative"; return "zero"; } public static void main(String[] args) { System.out.println(classify(5)); System.out.println(classify(-2)); System.out.println(classify(0)); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
positive negative zero
The final return "zero" is the n == 0 case, but it is also required by the compiler: if no earlier return fires, this one must catch it.
3

Overload a method so the same name works for two parameter types. Java picks the matching signature automatically, the int call returns an int, the double call returns a double.

class Main { static int multiply(int a, int b) { return a * b; } static double multiply(double a, double b) { return a * b; } public static void main(String[] args) { System.out.println(multiply(3, 4)); System.out.println(multiply(2.5, 2)); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
12 5.0
The compiler chooses the overload from the argument types, not the return type. Two methods cannot share a name and parameters but differ only in return type.

Data Flow

Arguments are passed by value; the method body computes and returns a value to the caller.
1 Call the method with arguments
↓
2 Parameters bound to copies
↓
3 Body executes and computes
↓
4 return passes the value back
Why this works
Pass by value

Java always passes by value. For primitives, the value is copied. For objects, the reference is copied, the method can mutate the object but not rebind the caller's variable:

static void bump(int x) {
    x += 10;   // only the local copy changes
}

int n = 5;
bump(n);
// n is still 5

To change a caller's value, return it or wrap it in a mutable object.

When you have a choice

Static vs instance methods

Static methods belong to the class and cannot access instance fields. Instance methods need an object:

class MathTools {
    static int square(int n) { return n * n; }   // no instance needed

    int value = 1;
    int increment() { return value + 1; }   // needs an object
}

Use static for helpers that don't need state; use instance methods for behaviour tied to an object.

Examples

Defining a method

A simple method

public static int add(int a, int b) {
    return a + b;
}

int result = add(3, 4);   // 7

public static makes the method callable without an instance. Parameters flow in; return sends a value back.

Overloading methods

Same name, different signatures

static double multiply(double a, double b) {
    return a * b;
}

static int multiply(int a, int b) {
    return a * b;
}

Method overloading lets multiple methods share a name if their parameter types differ. The compiler picks the matching one.

Returning values

Every path must return

static String classify(int n) {
    if (n > 0) return "positive";
    if (n < 0) return "negative";
    return "zero";   // required for n == 0
}

A method with a return type must reach a return on every path. A void method needs none.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Arrays and Lists
Previous Lesson
Arrays and Lists
Next Lesson
Exception Handling
Exception Handling →