A method is a reusable block of code. Write it once, and call it wherever you need it. Methods help you avoid duplicated code, keep your program clean, and break logic into manageable pieces. Cramming all your code into main without methods is like building a whole house with no separate rooms — hard to find anything, hard to fix anything.
public class Main {
static void greetUser(String name) {
System.out.println("Hello, " + name + "!");
}
static int calculateTotal(int price, int quantity) {
return price * quantity;
}
public static void main(String[] args) {
greetUser("Aung");
int total = calculateTotal(20, 3);
System.out.println("Total: " + total);
}
}greetUser method takes a name parameter and outputs a greeting. calculateTotal method takes price and quantity, and uses return to give back the multiplication result.
Hello, Aung! Total: 60Real-World Use
Splitting out logic like calculating payment totals, validating users, generating reports, or sending emails into methods matters a lot in production code.