The Method Header and How a Method Works
Where you are: Week 0 review > The method header
Try This First
Decide whether this header is legal before reading on:
public int static larger(int a, int b)
Reveal
It is not legal. The order is fixed: modifiers, then return type, then name, then parameters. The keyword static cannot sit after the return type. The legal form is public static int larger(int a, int b).
Before You Start
Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.
See Variables and the Primitive Types if any box above gave you pause.
Not sure? Take the 60-second self-check.
Try each from memory, then read the answer under it.
- Name two primitive types you could use for a method return value. Any of
int,double,boolean, orchar. (Stringis a type too, but not a primitive.) - What does a declared type fix about a variable? The kind of value it can hold; that type cannot change after declaration.
What You Need To Walk In With
Walk into the next class able to state these:
- A method is a named operation: fixed typed inputs in, one typed output out, or
voidfor a side effect that returns nothing. - The header order is fixed:
public static ReturnType name(Type param). Rearranging the words is a compile error. - A typed (non-
void) method mustreturna value on every path that can reach the end. - Calling a method substitutes its returned value back into the expression at the call site.
You should be able to: write a correct header from a description, and say whether a method should be void or typed.
How It Works
The six slots of a header
public static ReturnType name(Type param, Type param) {
| Slot | Example | Meaning |
|---|---|---|
| Access modifier | public |
visible to callers |
static |
static |
belongs to the class, not an object (this changes once classes arrive) |
| Return type | int |
the type handed back; void means nothing is returned |
| Name | larger |
a verb or noun phrase that states the job |
| Parameters | (int a, int b) |
zero or more Type name pairs, comma separated |
| Body | { ... } |
braces follow the ), never a semicolon |
The order is fixed. public int static foo(int n) does not compile.
A typed method returns a value
public static int larger(int a, int b) {
if (a >= b) {
return a;
}
return b;
}
Calling larger(10, 7) runs the body and substitutes 10 back into the call site. A typed method must return a value on every path that reaches the end; a println in a branch does not satisfy that rule.
A void method performs a side effect
public static void printBanner(String message) {
System.out.println("== " + message + " ==");
}
A void method returns nothing. It may use a bare return; to exit early, but it must not return a value.
Worked Example: Predict, Then Check
public static int doubled(int n) {
return n * 2;
}
System.out.println(doubled(6));
Predict the output before reading on.
Reveal
12. The call doubled(6) runs the body, returns 6 * 2, and that 12 is substituted into the println.
A Common Mistake
A typed method that returns a value in one branch but only prints in another does not compile: the message is “missing return statement.” Every path that can reach the end of a typed method must end in a return (or throw). Add a return to the branch that lacks one. (Source: BJP (Reges and Stepp), Ch 3; JLS 25, §8.4.7.)
Go Deeper (optional)
For the curious: static is on every CS1 method because there is no object yet to call the method on. Once classes arrive, most methods drop static and run on a specific object, so the same name can behave differently depending on which object receives the call. The header slots stay in the same order; only the static slot changes.
Check Yourself
Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.
Check your understanding
Which method header is legal?
A method returning int returns in the if branch but only prints in the else. Does it compile?
When should a method be declared void?
doubled(int n){ return n*2; } print(doubled(6)). What prints?