Thuta Learning
IntermediateProgrammingbeginner

Methods

Relax. We'll talk through this in plain words — no textbook voice.

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.

java
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.

You should see
Hello, Aung! Total: 60

Real-World Use

Splitting out logic like calculating payment totals, validating users, generating reports, or sending emails into methods matters a lot in production code.

Easy traps

  • Common mistakes include forgetting to write a return statement in a method that has a return type, or mixing up the parameter order.
Methods | Thuta Learning