← All lessons
Java basics · Lesson 8 of 10

Methods

You've been calling methods all along: println, length, Math.max. A method is a named piece of code that does one job. Writing your own lets you give a job a name, use it as often as you like, and fix it in one place.

Methods go inside the class, next to main (not inside it). This one is called greet. main calls it twice by writing its name with brackets:

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

    public static void main(String[] args) {
        greet("Ana");
        greet("Ben");
    }
}
Prints
Hello, Ana!
Hello, Ben!

String name in the brackets is a parameter: a variable the method gets from whoever calls it. greet("Ana") runs the method with name set to "Ana". A method can take several parameters, separated by commas, and the call must pass them in the same order. (static is needed for now because main is static; the next course explains it.)

Returning a value

void means the method gives nothing back. A method can instead return a value: write the type of the result in place of void, and use return to hand it back. The call then stands for that value, so you can store it, print it or use it in a sum.

Java
public class Main {
    static int square(int n) {
        return n * n;
    }

    static double average(int a, int b) {
        return (a + b) / 2.0;
    }

    public static void main(String[] args) {
        int s = square(4);
        System.out.println(s);
        System.out.println(square(3) + square(4));
        System.out.println(average(7, 8));
    }
}
Prints
16
25
7.5

return also ends the method straight away, so a method can return early. And a method that returns a boolean reads nicely in an if:

Java
public class Main {
    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static String sizeOf(int n) {
        if (n < 10) return "small";
        if (n < 100) return "medium";
        return "large";
    }

    public static void main(String[] args) {
        if (isEven(6)) System.out.println("6 is even");
        System.out.println(sizeOf(42));
    }
}
Prints
6 is even
medium

Where variables live

A variable made inside a method (or inside any { } block) only exists there; this is called its scope. Each method has its own variables, so a total in one method has nothing to do with a total in another. To get a value out, return it.

Parameters get a copy of the value passed in, so changing a parameter inside the method doesn't change the caller's variable:

Java
public class Main {
    static void addTen(int x) {
        x = x + 10;
        System.out.println("inside: " + x);
    }

    public static void main(String[] args) {
        int n = 5;
        addTen(n);
        System.out.println("outside: " + n);
    }
}
Prints
inside: 15
outside: 5

A good method does one thing and its name says what, often as a verb: printReceipt, isValidEmail, totalPrice. If you can't name it simply, it may be doing too much.

Practice

What does this print?

public class Main {
    static int triple(int n) {
        return n * 3;
    }

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

Quick reference

static void name(Type p) { }A method that takes p and returns nothing.
static int name(...) { return x; }A method that gives back an int.
name(value)Call it, passing the value in.
parameterA variable the method gets from its caller; it holds a copy of the value.
returnHand back a value and leave the method.
scopeWhere a variable exists: inside the block it was made in.

Related courses