This discussion is archived
4 Replies Latest reply: Jun 12, 2010 1:44 AM by 843798 RSS

Select element from array returned by java.lang.reflect.Method?

843798 Newbie
Currently Being Moderated
I would like to invoke a method that returns an array and then just pick the first element.

So, instead of doing
int myArr = myObj.getMyArray()[0];{code}
I would like to use reflection.

Method method = myObj.getClass().getMethod("getMyArray", new Class[]{});
int myArr = method.invoke(myObj,  new Class[]{})[0]); // array required, but java.lang.Object found
{code}How can I get the method to return an element of an array?