Consider this code:
class Foo {
public void doIt(String... strs) {
System.out.println("this is varargs");
}
private void doIt(String str) {
System.out.println("this is single");
}
}
class Bar {
public static void main (String[] args) {
new Foo().doIt("");
}
}
With javac version 1.6.0_29, it fails to compile, stating:
VarArgsError.java:14: doIt(java.lang.String) has private access in Foo
new Foo().doIt("");
^
1 error
Yes, this is silly code and there are at least two obvious workarounds, but I'm curious. Based on section 15.12.2 of the specification, this compilation error seems like a bug in javac, because the first step should remove the non-varargs doIt, as it is inaccessible according to section 6.6.1. Am I missing some other details in the lookup algorithm or is this as obviously wrong as I think it is?
The process of selecting the most appropriate overloaded function or operator is called overload resolution. Suppose that f is an overloaded function name. When you call the overloaded function f() , the compiler creates a set of candidate functions.
It generates this error message when one overload is more specific for one argument's data type while another overload is more specific for another argument's data type.
Just as a reminder, overloading is what happens when you have two methods with the same name but different signatures. At compile time, the compiler works out which one it's going to call, based on the compile time types of the arguments and the target of the method call.
Method Overloading and Operator Overloading are examples of the same. It is known as Early Binding because the compiler is aware of the functions with same name and also which overloaded function is tobe called is known at compile time.
You're not missing anything. This is a known bug.
As Qwe commented, here is a link to an appropriate bug filing: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6746184
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