Why do you think that it should give a compile time error?? The method method(null) matches the string version and prints string as o/p. If you remove the static method that takes String as argument, it prints object as the O/P. It's a good question. Can anyone please explain this behaviour.
Regards, Jothi Shankar Kumar. S
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
The null Literal can be cast into any type of Object Reference. So any one of the given method()'s can be called but the compiler tries to choose the most specific one.
Now the null matches the call for method(Object ) then it also matches the call for method(String )
since the string version is more specific the call is made to the method(String ) version.
But if you overload the method with Integer metod(Integer ) then the complier does not know which one to call because The String version is as specific as Integer version so the compiler shouts that the call is "AMBIGUOUS"