Defining and Calling Methods

In Java, a method is a block of code that performs a specific task. Methods are defined using the syntax:

public static void sayHello() {
  System.out.println("Hello, world!");
}

To call a method, use its name followed by parentheses:

sayHello();
← PrevNext →