Lesson · Methods
Methods: the signature is the contract
11 min readMethods
Declaration anatomy, what a signature is and is not, static versus instance, and the return-path rules the compiler enforces. Read a method header and you have read its contract.
Anatomy of a method
Read a method left to right: modifiers, return type, name, parameter list, body. The modifiers say who can call it and on what; the return type says what a caller gets; the name and parameters say what it means; the body is the only part callers never see.
void means no value comes back, and the compiler enforces that a non-void method returns on every path. Every branch either reaches a return value; or throws; a missing path is a compile error, which is why Java methods cannot silently fall off the end the way a forgotten return does in C.
Method lab
pick a declarationThe signature is the name plus the parameter types in order. Modifiers and return type are not in it; this is what the compiler matches a call against.
Read a method header and the signature, the receiver question and the return obligation are all answered.
The signature: name plus parameters
A method's signature is its name plus the types of its parameters, in order. Return type is NOT part of the signature, which is why int f() and void f() cannot coexist: same signature, and the compiler rejects the pair. The same rule explains why overloads must differ in parameters rather than in what they return.
Two methods differing like f(int) and f(Integer) have different signatures and are legal overloads, which is the box-against-primitive trap from the wrappers chapter showing up in method resolution. And f(int...) is a different signature again: the varargs form is its own method.
static versus instance
Instance methods run against a receiver (the object the call is sent to; this points at it). Static methods belong to the class: no receiver, no this, and no direct access to instance state. That is the whole distinction, and it explains the two errors everyone meets: calling an instance method from static context without an object, and reading an instance field from a static method.
Choose static when the method does not need object state: utilities, pure functions, small factories. Choose instance when the behaviour depends on fields. A method that never touches fields is static in everything but keyword, which is a code smell the static analysers happily point out.
Designing the contract
A good method does one thing, named with a verb, with parameters that are specific enough to constrain misuse: transfer(Account from, Account to, Money amount) reads like the sentence it implements. Return early over nesting pyramids, and prefer few parameters over many: beyond three or four, callers confuse positions, and a small parameter object reads better.
Avoid methods that both compute and log and mutate and print. Side effects should be the method's headline, not a footnote: printReport may print, buildReport should not. The caller decides what to do with the value.
Return type is not part of the signature
int size() and void size() cannot coexist in one class: same name, same parameter list, same signature, and overloading never looked at return types. Two methods are legal overloads only when their parameter lists differ. This rule is old, exact, and a reliable interview first question.
Interview angles
What exactly is a method signature?+
Name plus parameter types in order. Modifiers, return type and exceptions are not in it. Overload resolution and JVM lookup both key on it, which is why return-type-only variations are compile errors.
static versus instance methods, one sentence each?+
Instance methods run on a receiver and can read or change its fields; static methods belong to the class with no receiver and cannot touch instance state directly. If a method never uses fields, static is the honest keyword.
Does every path through a non-void method need a return?+
Yes, or a throw. The compiler checks all reachable paths, so a condition with no else and no trailing return does not compile. This is definite return, the flow cousin of definite assignment.