Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass object array as parameter in Java

The method is public static void method(Object[] params), how should I call it in the following scenarios?

  1. with one object as parameter ClassA a
  2. with more than one objects as parameters ClassA a, ClassB b, ClassC c? thank you
like image 908
derrdji Avatar asked May 10 '10 19:05

derrdji


1 Answers

You can create the array of objects on the fly:

method(new Object[] { a, b, c});

Another suggestion is that you change the signature of the method so that it uses java varargs:

public static void method(Object... params)

Nice thing is that it is compiled into a method with the same signature as above (Object[] params). But it may be called like method(a) or method(a, b, c).

like image 178
aioobe Avatar answered Sep 20 '22 12:09

aioobe