3

I need some help using reflection in Java. I need to use reflection to call a method that returns an Object array. Method.invoke() only returns an Object. How is this done?

Many thanks for any insights!

2 Answers 2

6

You just have to cast the return value of Method.invoke() to whatever you happen to know it is. Not very robust, but such are the risks of reflection! So in this case, it would be:

Object[] result = (Object[]) method.invoke(...);

Btw, note that if the method returns a primitive (int, double, etc), Method.invoke will return its boxed equivalent (Integer, Double, etc).

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

1 Comment

@user1084636: Accept one of these as the answer!
0

An Object[] is an object--cast the result of invoke to Object[].

Comments

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.