Card 02/ 10
Method Signature Example: Reading public static int add
Five decisions packed into one line, each word answering one question.
A method's declaration line says who may call it, whether it needs an object, what it returns, what it is called and what it needs, in that order. One line from a real class, read left to right:
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 a 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.
Notice that the parameter list is the only part that decides which method a call matches. The return type is not part of that decision at all.
← → move through sections and cards