Bro, In java, we use to call methods by their name and their parameters only to use them in our code, like
myMethod(20, 40)
so, JAVA only searches for similar stuff matching in their corresponding declaration (name + param), this is why method signature only includes method's name and parameters. :)
In JAVA and many other languages, you can call a method without a variable to hold the return value. If return type is part of a method signature, there is no way to know which method will be called when calling without specifying variable holding return value.
The compiler ignores it when has to check for duplicates. For Java is illegal to have two methods with the signature differing only by the return type.
Try that:
public class Called {
public String aMethod() {
return "";
}
}
public class Caller {
public static void main(String[] main) {
aMethod();
}
public static void aMethod() {
Called x = new Called();
x.aMethod();
}
}
Build the project, go to bin directory, copy the Caller.cass somewhere. Then change the called method:
public int aMethod() {
return 0;
}
Build the project, you will see that both Called.class and Caller.class have a new timestamp. Replace the Caller.class above and run the project. You'll have an exception:
If you try to run the code you have mentioned on eclipse, you will have an answer as to what elements does java compiler looks for differentiating among java methods :
class Foo {
public int myMethod(int param) {
return param;}
public char *myMethod*(int param) { //this line throws an error
return param;
}
}
The error thrown is :
Duplicate method myMethod(int) in type Foo .