TExcelTAD
Home › Java › Java Methods

Java Methods

A method is a named block of code that runs when it is called. Methods let you write a piece of logic once and reuse it many times, give that logic a descriptive name, and break a big problem into small, testable steps. You have been using methods all along: main is one, and so are println and Math.max.

Declaring and calling a method

java · try it yourself
class Main {
    static void printBanner() {
        System.out.println("********************");
        System.out.println("*  Welcome aboard  *");
        System.out.println("********************");
    }

    public static void main(String[] args) {
        printBanner();
        System.out.println("Loading your profile...");
        printBanner();
    }
}
********************
*  Welcome aboard  *
********************
Loading your profile...
********************
*  Welcome aboard  *
********************

The parts of the declaration static void printBanner():

Part Meaning
static belongs to the class, so main (also static) can call it directly
void the method returns no value
printBanner the name, written in camelCase, usually a verb
() the parameter list — empty here
{ ... } the body, the code that runs on each call

Methods are declared inside a class but outside other methods. The order of methods in the class doesn't matter.

Returning a value

Replace void with a type, and use return to send a value back to the caller. The call itself then becomes an expression you can store, print or combine:

java · try it yourself
class Main {
    static double circleArea(double radius) {
        return Math.PI * radius * radius;
    }

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

    public static void main(String[] args) {
        double a = circleArea(2.0);
        System.out.printf("Area: %.2f%n", a);
        System.out.printf("Two circles: %.2f%n", circleArea(1) + circleArea(3));
        System.out.println("10 is even: " + isEven(10));
        System.out.println("7 is even: " + isEven(7));
    }
}
Area: 12.57
Two circles: 31.42
10 is even: true
7 is even: false

return ends the method immediately. A non-void method must return a value on every possible path, otherwise the compiler complains with missing return statement.

Early return

Returning early often makes code flatter and easier to read than deeply nested ifs:

java · try it yourself
class Main {
    static String classify(int age) {
        if (age < 0) return "invalid";
        if (age < 13) return "child";
        if (age < 20) return "teenager";
        return "adult";
    }

    public static void main(String[] args) {
        int[] ages = {-1, 8, 15, 42};
        for (int age : ages) {
            System.out.println(age + ": " + classify(age));
        }
    }
}
-1: invalid
8: child
15: teenager
42: adult

A void method can also use a bare return; to stop early.

Methods calling methods

Good programs are built from small methods that call each other, each doing one clear job:

java · try it yourself
class Main {
    static double celsiusToFahrenheit(double c) {
        return c * 9 / 5 + 32;
    }

    static String describe(double c) {
        double f = celsiusToFahrenheit(c);
        String feel = c >= 25 ? "warm" : "cool";
        return String.format("%.1f C = %.1f F (%s)", c, f, feel);
    }

    public static void main(String[] args) {
        System.out.println(describe(30));
        System.out.println(describe(12.5));
    }
}
30.0 C = 86.0 F (warm)
12.5 C = 54.5 F (cool)

Tip: If you can't describe what a method does in one short sentence, it is probably doing too much — split it.

Static vs. instance methods

All methods in this chapter are static: you call them without creating an object. Methods without static belong to objects (like "hello".toUpperCase()), and you'll write those in the Classes section.

Test yourself

What does the return type void mean?

  1. The method takes no parameters
  2. The method returns nothing
  3. The method is empty
  4. The method is private

What is printed? static int twice(int x) { return x * 2; } ... System.out.println(twice(twice(3)));

  1. 6
  2. 9
  3. 12
  4. 36

Where may a method be declared?

  1. Inside another method
  2. Inside a class, outside other methods
  3. Outside any class
  4. Only inside main