| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
Why this method can not be overloaded?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
Why this method can not be overloaded?Consider the following code in Java 5.0, why the function() method can not be overloaded?
public class Program {
void function(String... names){
System.out.println("in function(String... names)");
}
void function(String[] names){
System.out.println("in function(String[] names)");
}
}
In the first function() method, the names argument is defined as type String.... This tells the compiler that calling code can pass a variable number of String parameters. At compile time a vararg is converted to an array (see USING THE VARARGS LANGUAGE FEATURE). The String... equates to a String array (String[]). Therefore, the compiler will treat the second function() method as the duplicate method in the class. |