Aral Code

Arrays & Methods Lesson 15 of 21

Java Methods — Parameters, Return Values and Overloading

Organize code into reusable methods — define static methods, pass parameters, return values, overload methods, and understand pass-by-value and recursion.

A method is a named block of code that performs a task. Instead of writing the same code many times, you write it once inside a method and call it whenever you need it. main is a method; so are println and Math.sqrt.

Defining and calling a method

public class Main {
    static void printLine() {
        System.out.println("------------------------");
    }

    public static void main(String[] args) {
        printLine();
        System.out.println("  OFFICIAL RECEIPT");
        printLine();
    }
}

Output

------------------------
  OFFICIAL RECEIPT
------------------------
  • static means the method belongs to the class itself, so main can call it directly. (You'll learn non-static methods in the Classes lesson.)
  • void means the method doesn't return a value.
  • printLine is the name, in camelCase, usually a verb.

Parameters

public class Main {
    static void greet(String name, int grade) {
        System.out.println("Hello " + name + " of Grade " + grade + "!");
    }

    public static void main(String[] args) {
        greet("Ana", 11);
        greet("Ben", 12);
    }
}

Output

Hello Ana of Grade 11!
Hello Ben of Grade 12!

Each parameter has a type. The arguments you pass must match in number, order and type.

Return values

Replace void with the type of value the method gives back, and use return:

public class Main {
    static double average(double a, double b, double c) {
        return (a + b + c) / 3;
    }

    static String descriptor(double grade) {
        if (grade >= 90) return "Outstanding";
        if (grade >= 85) return "Very Satisfactory";
        if (grade >= 80) return "Satisfactory";
        if (grade >= 75) return "Fairly Satisfactory";
        return "Did Not Meet Expectations";
    }

    static boolean isPassing(double grade) {
        return grade >= 75;
    }

    public static void main(String[] args) {
        double avg = average(88, 91, 85);
        System.out.printf("%.2f %s (passed: %b)%n", avg, descriptor(avg), isPassing(avg));
    }
}

Output

88.00 Very Satisfactory (passed: true)

Methods with arrays

public class Main {
    static int highest(int[] values) {
        int max = values[0];
        for (int v : values) {
            if (v > max) max = v;
        }
        return max;
    }

    public static void main(String[] args) {
        int[] scores = {78, 95, 88, 91};
        System.out.println("Highest: " + highest(scores));
    }
}

Output

Highest: 95

Method overloading

Several methods can share a name if their parameter lists differ. Java picks the right one based on the arguments:

public class Main {
    static int add(int a, int b) { return a + b; }
    static double add(double a, double b) { return a + b; }
    static int add(int a, int b, int c) { return a + b + c; }

    public static void main(String[] args) {
        System.out.println(add(2, 3));
        System.out.println(add(2.5, 3.1));
        System.out.println(add(1, 2, 3));
    }
}

Output

5
5.6
6

println itself is overloaded: there are versions for int, double, String and more.

Pass by value

Java passes a copy of each argument. Changing a primitive parameter doesn't affect the caller's variable:

public class Main {
    static void tryToChange(int x, int[] arr) {
        x = 100;
        arr[0] = 100;
    }

    public static void main(String[] args) {
        int n = 5;
        int[] a = {5, 6};
        tryToChange(n, a);
        System.out.println(n + " " + a[0]);
    }
}

Output

5 100

n stays 5. But the array changed! The copy passed was a copy of the reference to the same array, so the method could change its contents.

Recursion

A method can call itself. Each call works on a smaller problem until it reaches a simple base case:

public class Main {
    static long factorial(int n) {
        if (n <= 1) return 1;          // base case
        return n * factorial(n - 1);   // recursive case
    }

    public static void main(String[] args) {
        System.out.println(factorial(5));
    }
}

Output

120

factorial(5) = 5 × factorial(4) = 5 × 4 × factorial(3) … = 120. Without a base case, recursion never stops and crashes with StackOverflowError.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does void mean in a method header?
2. Which header correctly returns a double?
3. What is method overloading?
4. What does every recursive method need?

By Sir Reyel · Last updated 2026-12-04