CodeMIRAI
0 XP

Methods

2 min read 4 runnable examples Quiz included

A method is a named block of code you can run whenever you need it. main is a method; here's how to write your own.

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

    public static void main(String[] args) {
        greet("Asha");
        greet("Leo");
    }
}
  • static means the method belongs to the class itself, so main can call it directly. (Methods that belong to objects come in the classes lesson.)
  • void means the method doesn't return anything.
  • String name is a parameter: a variable that receives the value passed in, called the argument.

Returning a value

Java
public class Main {
    static double area(double width, double height) {
        return width * height;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    public static void main(String[] args) {
        double room = area(5.0, 3.5);
        System.out.println("Room: " + room + " square metres");
        System.out.println(isEven(10) + " " + isEven(7));
    }
}

The return type, here double or boolean, comes before the method's name, and return sends a value back to the caller. A method with a return type must return a value on every path through it, or it won't compile.

Methods calling methods

Java
public class Main {
    static double toFahrenheit(double celsius) {
        return celsius * 9 / 5 + 32;
    }

    static String feel(double celsius) {
        if (celsius >= 30) return "hot";
        if (celsius >= 15) return "mild";
        return "cold";
    }

    static String describe(double celsius) {
        return celsius + " C is " + toFahrenheit(celsius) + " F, which feels " + feel(celsius);
    }

    public static void main(String[] args) {
        System.out.println(describe(35));
        System.out.println(describe(20));
        System.out.println(describe(-5));
    }
}

Small methods that each do one job, combined by others, are far easier to read, test and fix than one long main. A return also ends the method immediately, which is what lets feel skip the ifs it doesn't need.

Scope

A variable declared inside a method, or inside any { } block, exists only there:

Java
static void demo() {
    int x = 5;
    if (x > 0) {
        int y = x * 2;    // y exists only inside this if
    }
    // y doesn't exist here, and x doesn't exist outside demo()
}

Pass by value

Java
public class Main {
    static void tryToChange(int number, int[] numbers) {
        number = 100;        // changes this method's copy only
        numbers[0] = 100;    // changes the array both variables refer to
    }

    public static void main(String[] args) {
        int n = 1;
        int[] arr = {1, 2, 3};
        tryToChange(n, arr);
        System.out.println(n);         // 1
        System.out.println(arr[0]);    // 100
    }
}

Java always passes a copy of the value. For a primitive like int, that's a copy of the number, so the caller's variable can't change. For an array or an object, it's a copy of the reference, so the method can change the object both refer to, but not make the caller's variable point somewhere else.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.