Card 02/ 10
All 10 cards
ExampleDifficulty: Beginner1 min
Reading a Method Signature From Left to Right
One line from a real class, with five separate decisions packed into it. Read left to right and each word answers one question.
public static int add(int a, int b) {
return a + b;
}| Part | Question it answers |
|---|---|
public | Who may call it |
static | Does it belong to the class rather than to one object |
int | What does it hand back |
add | What do callers write |
(int a, int b) | What must callers supply, and in what order |
Two of those are worth reading carefully. The return type is the promise: declaring int means every path through the body has to end in a return of an int, and the compiler checks every path. Declaring void means the opposite promise — the method produces nothing, and return someValue; inside it will not compile.
What to notice: the parameter list is the only part that decides which method a call matches, and the return type is not part of that decision at all.