Card 02/ 10

ExampleDifficulty: Beginner1 min

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:

java
public static int add(int a, int b) {
    return a + b;
}
Each part of the signature, and the question it answers
PartQuestion it answers
publicWho may call it
staticDoes it belong to the class rather than to one object
intWhat does it hand back
addWhat 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.