Defining Methods
A method gives a block of behavior a name. Calling that name runs the method body and then returns control to the caller.
Small methods reduce repetition and let readers understand intent before implementation details. greet() says more than repeating its print statement at every call site.
Name a piece of behavior
public class Main {
static void greet() {
System.out.println("Game loaded");
}
public static void main(String[] args) {
greet();
}
}
Output:
Game loaded
void means the method does not return a value. static lets main, which is also static, call it without creating a Main object.
Method names conventionally begin with a lowercase letter and describe an action, such as calculatePay or printBadge.
Complete showMessage and call it from main to welcome the learner.