Return Values

A method can calculate a value and return it to its caller. Its declared return type tells the compiler what every successful path must produce.

Returning a result separates calculation from presentation. Another caller could store the minute count, compare it, or format it differently.

Send a result back

public class Main {
    static int toMinutes(int seconds) {
        return seconds / 60;
    }

    public static void main(String[] args) {
        int seconds = 300;
        System.out.println(toMinutes(seconds));
    }
}

Output:

5

toMinutes returns an int, and main passes that returned value directly to println.

A non-void method must return on every reachable path. Missing a return is a compile-time error.

Return the number of complete hours in the supplied minute count.