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.
()
// 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.
greet
Hi!
▶ 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.
sayHi()
sayHi();
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(); } }