Skip to lesson

Define and call a method

To define a method, you write its header and its body. To call it, you write its name with brackets, like greet();.

The parts of a method

A greeting is needed in many places, so it gets its own method. The first line, the header, gives the method its name; the lines inside { }, the body, run on every call.

Java
static void greet() {    System.out.println("Good morning, students!");}
The header static void greet() with each part labelled: static, write it for now; void, gives back no value; greet, the name, a verb in camelCase; the empty brackets, where values go in, covered next.
You may also see public static void greet(). Both work in these programs; for now, always write static.

Where a method goes

Write greet inside main, and Java stops with illegal start of expression. A method goes inside the class but outside every other method, above or below main.

Example 1 · A method above main

Greeting.javaJava
public class Greeting {    static void greet() {        System.out.println("Good morning, students!");    }
    public static void main(String[] args) {        greet();    }}
Output
Good morning, students!

What this code does

  • greet sits inside the class, between its opening { and main. It could just as well go after the closing } of main.
  • Java always starts at main, wherever it is in the file, so the greeting prints once: from the call.

Find the bug

Spot the method bug

Select the line that is wrong.

This should print Hello!

Round 1 of 4

What happens at a call

main prints a line, calls greet(), then prints again. In what order do the lines appear? A call makes Java jump into the method, run its whole body, and come back to the line after the call.

Trace the code

Follow Java into the method and back

public class CallOrder {
public static void main(String[] args) {
System.out.println("Class starts");
greet();
System.out.println("Class ends");
}
static void greet() {
System.out.println("Good morning, students!");
}
}

Variables

None yet.

Output

Class starts

Line 3Java starts at main and runs its first line.

Step 1 of 5
A method runs only when it is called. A method that nothing calls never runs, even if it sits right above main.

A method can call another method

A class list starts with a header: a line, a title, another line. Any method can call another, so printHeader() calls printLine(), and each call returns to the method that made it.

Example 2 · A method that calls another

Header.javaJava
public class Header {    public static void main(String[] args) {        printHeader();        System.out.println("Roll 1: Aman");    }
    static void printHeader() {        printLine();        System.out.println("CLASS 12 - B");        printLine();    }
    static void printLine() {        System.out.println("------------");    }}
Output
------------
CLASS 12 - B
------------
Roll 1: Aman

What this code does

  • main calls printHeader, and printHeader calls printLine before and after the title.
  • When printLine ends, Java goes back to printHeader, not to main.
  • Only when printHeader ends does Java return to main and print Roll 1: Aman.

Name a method with a verb in camelCase, like printHeader, showMenu or calculateTotal, so that a call reads like an instruction (the same style as variable names (opens in a new tab)).

Next step

Check your understanding

8 quick questions