Method Signature
In a class with two students called Navika, the teacher adds a surname: Navika Anamaneni and Navika K. Java has the same problem with methods: a class can have several methods with the same name, so it needs something more to tell them apart. That something is the method signature: the method's name plus the types of its parameters, in order.
What's in the Signature
| Part of the declaration | In the signature? |
|---|---|
| Method name | Yes |
| Number, types and order of parameters | Yes |
| Return type | No |
| Parameter names | No |
Modifiers (public, static) | No |
throws clause (covered with exceptions, later in the course) | No |
So fare(int, int) is the signature of static int fare(int km, int people).
Same Name, Different Signatures: Overloading
Because the signatures differ, a class can have several methods named fare. This is called method overloading:
public class Fares {
static int fare(int km) {
return km * 2;
}
static int fare(int km, int people) {
return km * 2 * people;
}
static double fare(double km) {
return km * 2.5;
}
public static void main(String[] args) {
System.out.println(fare(100));
System.out.println(fare(100, 3));
System.out.println(fare(100.0));
}
}200
600
250.0How it works
- The three signatures are
fare(int),fare(int, int)andfare(double): all different. - For each call, the compiler picks the method whose parameters match the arguments: one
int, twoints, or onedouble. - This choice happens at compile time.
Try it yourself: call fare(100L) with a long. Which method is chosen, and why? (Hint: a long can widen to double but not to int.)
When Signatures Clash
Changing only the return type doesn't make a new signature, so this doesn't compile:
O.java:3: error: method fare(int) is already defined in class O
static double fare(int km) { return km; }
^The same happens if only the parameter names differ: fare(int km) and fare(int distance) have the same signature.
Common Mistakes
Thinking the return type distinguishes methods. It doesn't; the compiler wouldn't know which to call from fare(5);.
Overloads that differ only in parameter order of different types. book(int seat, String name) and book(String name, int seat) are legal but easy to call by accident. Avoid them.
Ambiguous calls. With show(long) and show(double), calling show(5) works (it picks long, the closer widening), but such overloads confuse readers.
Interview Corner
What is a method signature in Java?
The method's name together with the number, types and order of its parameters. The return type, parameter names, modifiers and thrown exceptions are not part of it.
Can two methods differ only in return type?
No. They'd have the same signature, and the compiler reports method ... is already defined. Overloaded methods must differ in their parameter lists.
Practice
- Easy: Write the signature of
public static boolean isValid(String pin, int attempts). - Harder: Write three overloaded
areamethods: for a square (onedouble), a rectangle (twodoubles) and a circle (onedoublenamedradius). Why doesn't the third compile, and how would you fix it?
Key Takeaways
Was this tutorial helpful?