Re: java - invoke
KeeKee wrote:
I see some of programs as below, and I don't quite understand. What
are the purposes of calling invoke and how to use it ? Thanks a lot.
case 1)
method = obj.getClass().getMethod(methodName, param1.class,
Here we need to know the type of the variable 'method'. Based on the
expression assigned to it, we can safely guess that the type is
'java.lang.reflect.Method'.
param2.class, ..);
method.invoke(obj, arg1, arg2,...);
case 2)
Class c = Class.forName("java.text.NumberFormat");
Method m = c.getMethod("getInstance");
Object ret = m.invoke(null);
Same here - we need to think of the type of the variable 'm', which is
'java.lang.reflect.Method'.
case 3)
Class c = Class.forName( args[0] );
Method m = c.getMethod( args[1], new Class [] { } );
Object ret = m.invoke( null, null );
Now that we are thinking of 'java.lang.reflect.Method', we look up the
description of its 'invoke()' method in the Javadocs to achieve
perfect understanding.
<http://java.sun.com/javase/6/docs/api/java/lang/reflect/
Method.html#invoke(java.lang.Object,%20java.lang.Object...)>
--
Lew