I was tracking down a bug today, and I noticed something strange within one of our classes. I cut out as much code as possible to post here:
class A {
static int obtainNumber() { return 42; }
static int obtainNumber() { return 3; }
static int obtainNumber() { return -1; }
static {
System.out.println(obtainNumber());
}
}
This class has 3 methods with the exact same name and signature. At first I thought this was invalid code, but then eclipse would have highlighted the code in red. It does work:
javac A.java && java A
42
Exception in thread "main" java.lang.NoSuchMethodError: main
So I figured maybe Java will just use the first one it sees. I reordered to test:
class A {
static int obtainNumber() { return 3; }
static int obtainNumber() { return -1; }
static int obtainNumber() { return 42; }
static {
System.out.println(obtainNumber());
}
}
Nope, same result:
javac A.java && java A
42
Exception in thread "main" java.lang.NoSuchMethodError: main
I thought perhaps it uses the one with 42 because its the biggest. To test this, I took the original and changed the return values:
class A {
static int obtainNumber() { return 0; }
static int obtainNumber() { return 1; }
static int obtainNumber() { return 2; }
static {
System.out.println(obtainNumber());
}
}
It still knows to use the first one:
javac A.java && java A
0
Exception in thread "main" java.lang.NoSuchMethodError: main
And if I reorder them again:
class A {
static int obtainNumber() { return 1; }
static int obtainNumber() { return 0; }
static int obtainNumber() { return 2; }
static {
System.out.println(obtainNumber());
}
}
Same result:
javac A.java && java A
0
Exception in thread "main" java.lang.NoSuchMethodError: main
I thought Java was a text based language, which I'd expect makes this sort of thing impossible. How is Java tracking which method is which?
For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
Two Methods cannot have same method signature. Methods can have same method name, and this process called method overloading.
We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return types.
I just copied/pasted this in my IDE and though it was a miracle, then when trying to save the file an error message appeared clearing this issue:
Save could not be complete. Try File > Save As... if the problem persists.
Reason: Some characters cannot be mapped using "Cp1252" caracter encoding. Either change the encoding or remove the characters which are not supported by the "Cp1252" caracter encoding.
So, these methods don't have the same name, just use characters that look the same.
More info related to character encoding on Java source files:
Hidden characters. The source code is equivalent to
static int obtainNumber() { return 42; }
static int obtain\ufeffNumber() { return 3; }
static int obtain\ufeff\ufeffNumber() { return -1; }
To avoid this kind of problems, my source files are strictly US-ASCII. I want to be certain that the characters I see are exactly the characters compiler sees.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With