Java variadic function parameters

Posted by Amir Rachum on Stack Overflow See other posts from Stack Overflow or by Amir Rachum
Published on 2010-04-14T06:12:53Z Indexed on 2010/04/14 6:23 UTC
Read the original article Hit count: 439

Filed under:

Hi,

I have a function that accepts a variable number of parameters:

foo (Class... types);

In which I get a certain number of class types. Next, I want to have a function

bar( ?? )

That will accepts a variable number of parameters as well, and be able to verify that the variables are the same number (that's easy) and of the same types (the hard part) as was specified in foo.

How can I do that?

Edit: to clarify, a call could be:

foo (String.class, Int.class);
bar ("aaa", 32); // OK!
bar (3); // ERROR!
bar ("aa" , "bb"); //ERROR!

Also, foo and bar are methods of the same class.

© Stack Overflow or respective owner

Related posts about java