FAQ

Java

JSP

Servlet


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.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |