10

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

1 Answer 1

29

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Beware, though, with the varargs variant, you'll have to explicitly cast to Object if you want to pas an Object[] as the only argument (and I don't think you'll get a warning if you forget).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.