A method is a named, reusable block of code that performs a specific task. Instead of writing the same logic over and over, you write it once inside a method and call the method by name whenever you need it.
Every method declaration has a few parts:
public or private, controls who can call the methodString or Integer (or void for nothing)public static String getGreeting() {
return 'Hello from Apex!';
}
This method is public, static, returns a String, is named getGreeting, and takes no parameters. The body returns a text value.
To run a method, write its name followed by parentheses. If the method returns a value, you can store it in a variable:
String message = getGreeting(); System.debug(message); // Hello from Apex!