Up to now everything has gone inside main, which works until it does not. A program that reads marks, validates them, averages them, finds the highest and prints a report is a few hundred lines in one block, and finding anything in it means reading all of it. A method is a named block of code you can run by writing its name. Once a job has a name, you can use it from three different places, test it on its own, and fix it in one spot instead of three.
- Reuse: write the code once, then call it as many times as you like from anywhere in the program.
- Naming:
calculateAverage(marks)says what is happening; eight lines of loop leave the reader to work it out. - Testing in isolation: a small method can be checked on its own with a few values, which is far easier than testing a whole program at once.
- DRY — Do not Repeat Yourself: duplicated code has to be fixed in every copy, and the copy you forget becomes the bug.
public static int square(int n) {
return n * n;
}
// | | | | |
// | | | | +-- parameter list: what it needs, and of what type
// | | | +--------- name: what it is called
// | | +--------------- return type: what kind of value it hands back
// | +--------------------- static: belongs to the class, not to an object
// +---------------------------- access modifier: who is allowed to call it| Part | Example | What it controls |
|---|---|---|
| Access modifier | public | Who may call the method. public means anywhere; private means only inside this class |
static | static | The method belongs to the class itself, so no object is needed to call it |
| Return type | int | The type of value handed back. void means nothing is handed back |
| Name | square | How you call it. By convention a verb in camelCase, starting lowercase |
| Parameter list | (int n) | The values the method needs, with a type and a name for each. Empty brackets mean it needs nothing |
void versus a returning method
A void method does something — it prints, it draws, it saves. A returning method produces something — a number, a string, a boolean — and hands it back to whoever called it. The difference matters at the call site: a returning method's result is a value, so it can be stored in a variable, used inside an expression or printed. A void call is a complete statement on its own and cannot appear on the right of an equals sign.
public class Greeter {
// void: does something, hands nothing back
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
// returning: produces a value the caller can use
public static int square(int n) {
return n * n;
}
public static void main(String[] args) {
greet("Amara"); // a whole statement on its own
int result = square(7); // the value is stored
System.out.println("7 squared is " + result);
System.out.println(square(3) + square(4)); // used directly in an expression
}
}
// Output:
// Hello, Amara!
// 7 squared is 49
// 25// Will NOT compile: missing return statement
public static int biggest(int a, int b) {
if (a > b) {
return a;
}
// what happens when a <= b? Java refuses to guess.
}
// Correct
public static int biggest(int a, int b) {
if (a > b) {
return a;
}
return b;
}A return does two things at once: it supplies the value and it ends the method on the spot. Nothing after it runs. That is what makes the chain of ifs below correct without any else — if the first condition is true the method is already finished, so each later check can safely assume every earlier one failed.
public static String levelFor(int mark) {
if (mark >= 80) {
return "Level 4"; // reaching here ends the method at once
}
if (mark >= 70) {
return "Level 3"; // only reached when mark < 80
}
if (mark >= 60) {
return "Level 2";
}
if (mark >= 50) {
return "Level 1";
}
return "Below Level 1"; // the catch-all path
}What happens when you call a method
A method call is a detour, not a jump. Java remembers precisely where it was, hands the argument values over, runs the method body from the top, and then comes back to the exact spot it left — with the returned value in hand, if there was one. The line that contained the call then finishes. Methods can call other methods, and Java keeps track of every level of detour, unwinding them in reverse order as each one returns.
| Step | What Java does |
|---|---|
| 1 | Reaches the call int result = square(7); and pauses this line |
| 2 | Evaluates the argument 7 and copies it into the parameter n |
| 3 | Runs the body of square, computing 7 * 7 |
| 4 | Hits return 49; — the method ends and 49 travels back to the call |
| 5 | The paused line resumes with 49 in place of the call, so result becomes 49 |
| 6 | Execution carries on with the next line of main |