How to work with varargs and reflection

Test.class.getDeclaredMethod("foo", String[].class);

works. The problem is that getMethod(..) only searches the public methods. From the javadoc:

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

Update: After successfully getting the method, you can invoke it using:

m.invoke(this, new Object[] {new String[] {"a", "s", "d"}});

that is – create a new Object array with one element – the String array. With your variable names it would look like:

m.invoke(this, new Object[] {a});

Leave a Comment