|
Java™ by example!
|
|
|
How do I execute a method using reflection?
The following example shows you how to. First get a Method instance using the getMethod method executed on any Class object of the class containing that method. Make sure you specify the correct method name and parameter list to uniquely identify the method. Then call invoke on that method instance and specify the object instance on which you want to execute that method and specify the parameters (wrap primitive types in their equivalent object types). If you want to execute a static method, you don't need to specify an object instance, it is ignored anyway (use null). Main.java:
outputs:
Further Information
Author of answer: Joris Van den Bogaert
Comments
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|