Task 2 of 8 · 0 solved · 8 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

3.1 Methods — calling a method

Lesson

Methods: naming a block of code

A method is a named block of code. Once it exists, you run it by calling it — writing its name followed by (). You can call the same method as many times as you like, without copying its code.


Worked example

// a method named greet
void greet() {
    System.out.println("Hi!");
}

// calling it twice
greet();
greet();

Each call runs the code inside greet, so this prints Hi! twice.


▶ Try it: the method sayHi() is already written for you (its code is hidden). Run this to call it, then add another sayHi(); line and re-run — each call runs the method again.



What does it mean to call a method?


sayHi() prints Hi!. How many lines does this print?

sayHi();
sayHi();
sayHi();

True or false?


Assemble a statement that calls a method named greet.


Type exactly what this program prints.

public class Main {
    static void beep() {
        System.out.println("beep");
    }
    public static void main(String[] args) {
        beep();
        beep();
    }
}