Lesson 76 min read

Methods

Write it once, use it everywhere

What's a Method?

A method is like a recipe card. Instead of explaining how to bake cookies every single time, you write the recipe once and just say "follow the cookie recipe." In Java, methods let you bundle up a chunk of code, give it a name, and call it whenever you need it.

When you call a method, Java pushes it onto the call stack; when it returns, it pops off. Every Java method has a few parts:

  • Return type — what does the method hand back? (int, String, void for nothing)
  • Name — what you call it (calculateTax, greet)
  • Parameters — what ingredients it needs (the inputs)
  • Body — the actual instructions inside { }

We use static methods for now so they can be called directly from main(). You'll learn about non-static methods when we get to classes.

Your First Methods

public class Main {
// void method — does something but returns nothing
static void greet(String name) {
System.out.println("Hey, " + name + "! Welcome aboard.");
}
// Method that returns a value
static int add(int a, int b) {
return a + b;
}
// Method with a boolean return
static boolean isEven(int number) {
return number % 2 == 0;
}
// Method with multiple lines of logic
static String getLetterGrade(int score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
return "F";
}
public static void main(String[] args) {
greet("Alex");
greet("Sam");
int sum = add(15, 27);
System.out.println("15 + 27 = " + sum);
System.out.println("Is 4 even? " + isEven(4));
System.out.println("Is 7 even? " + isEven(7));
System.out.println("Score 85 = " + getLetterGrade(85));
}
}
Output
Hey, Alex! Welcome aboard.
Hey, Sam! Welcome aboard.
15 + 27 = 42
Is 4 even? true
Is 7 even? false
Score 85 = B

Method Overloading — Same Name, Different Ingredients

Java lets you have multiple methods with the same name as long as they take different parameters. This is called overloading. It's like having a "make coffee" recipe — one version makes black coffee (no parameters), another makes a latte (milk parameter), and another makes a mocha (milk + chocolate).

Java figures out which version to call based on what arguments you pass in. It looks at the number and types of arguments.

Method Overloading in Action

public class Main {
// Version 1: multiply two ints
static int multiply(int a, int b) {
return a * b;
}
// Version 2: multiply three ints
static int multiply(int a, int b, int c) {
return a * b * c;
}
// Version 3: multiply two doubles
static double multiply(double a, double b) {
return a * b;
}
// Overloaded print helpers
static void printInfo(String name) {
System.out.println("Name: " + name);
}
static void printInfo(String name, int age) {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
System.out.println("2 * 3 = " + multiply(2, 3));
System.out.println("2 * 3 * 4 = " + multiply(2, 3, 4));
System.out.println("2.5 * 3.5 = " + multiply(2.5, 3.5));
printInfo("Luna");
printInfo("Luna", 12);
}
}
Output
2 * 3 = 6
2 * 3 * 4 = 24
2.5 * 3.5 = 8.75
Name: Luna
Name: Luna, Age: 12
Note: Methods are the heart of the DRY principle — Don't Repeat Yourself. If you find yourself copying and pasting code, that's a sign you should put it in a method. Bonus: if you need to fix a bug, you only fix it in one place instead of hunting down every copy.

Quick check

What does 'void' mean as a return type?
ArraysClasses & Objects