I have a interface and in the interface I want to declare a method such that it can take any number of objects as input.
Something like this:
interface Implementable{
public ReturnObj doIt(objects ....);
}
Please advise
The correct syntax would in your case be:
interface Implementable{
public ReturnObj doIt(Object... objs);
}
Official documentation for var-arg methods is found here .
I was about to ask the difference between varargs and passing an array,
Varargs gets compiled into an argument of an array-type. The only difference is with the vararg syntax, method calls such as
doIt("hello", "world");
will be compiled into
doIt(new Object[] { "hello", "world" });
In other words, given a declaration such as
public ReturnObj doIt(Object[] objs);
you'll have
doIt(new Object[] { "hello", "world" }); // works fine
doIt("hello", "world"); // won't compile
while given the var-arg declaration, both method calls will compile and be equivalent.
Pass an array:
public ReturnObj doIt(Object[] input);
or use the equivalent varargs expression
public ReturnObj doIt(Object... input);
Example:
interface Implementable{
public ReturnObj doIt(Object... object);
}
Alternatively (which I should prefer, especially in Web Services design):
interface Implementable{
public ReturnObj doIt(Object[] object);
}
You forgot to ask a question, but assuming you want to know how to declare a method which takes variable number of arguments, check out this link:
http://download.oracle.com/javase/1,5.0/docs/guide/language/varargs.html
So it would be
interface Implementable{
public ReturnObj doIt(Object... objects);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.