×

java · level 1 · unit 7 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A method is a recipe card with a name on top.

You have used methods like .length(). Now make your own. Write the recipe once, give it a name, then use it as often as you like, without copying a single line.

To one, write , a name, empty brackets and a block, inside Main but outside main. static lets main use it straight away, and means it hands nothing back. Defining it does not run it.

To it, write its name with brackets and a semicolon, like cheer();. Every line in its block then runs, top to bottom. Call it twice and it runs twice.

public class Main {
    static void cheer() {
        System.out.println("Go team!");
        System.out.println("You can do it!");
    }
    public static void main(String[] args) {
        cheer();
        cheer();
    }
}

Now you can read the frame: main is a method too, and Java calls it for you when you press run.