import java.lang.reflect.*;
class Foo
{
public void foo()
{
System.out.println("Foo.foo");
}
}
class Bar extends Foo
{
public void bar(String s)
{
System.out.println("Bar.bar: " + s);
}
}
class Invoke
{
public static void main(String[] args)
throws Exception
{
Bar b = new Bar();
Class clas = b.getClass();
// Invoke foo() reflectively
Method method = clas.getMethod("foo", null);
method.invoke(b, null);
// Invoke bar() reflectively
Class[] argTypes = new Class[] {String.class};
method = clas.getMethod("bar", argTypes);
Object[] argVals = new Object[] {"baz"};
method.invoke(b, argVals);
}
}
/* Output:
Foo.foo
Bar.bar: baz
*/
End of Listing