java.lang.reflect.Method.invoke() Method Example

The java.lang.reflect.Method.invoke(Object obj, Object... args) method invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.
Following is the declaration for java.lang.reflect.Method.invoke(Object obj, Object... args) method.
public Object invoke(Object obj, Object... args)
   throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException

obj − the object the underlying method is invoked from.
obj − the object the underlying method is invoked from.
args − the arguments used for the method call.
args − the arguments used for the method call.
the result of dispatching the method represented by this object on obj with parameters args.
IllegalAccessException − if this Method object is enforcing Java language access control and the underlying method is inaccessible.
IllegalAccessException − if this Method object is enforcing Java language access control and the underlying method is inaccessible.
IllegalArgumentException − if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.
IllegalArgumentException − if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.
InvocationTargetException − if the underlying method throws an exception.
InvocationTargetException − if the underlying method throws an exception.
NullPointerException − if the specified object is null and the method is an instance method.
NullPointerException − if the specified object is null and the method is an instance method.
ExceptionInInitializerError − if the initialization provoked by this method fails.
ExceptionInInitializerError − if the initialization provoked by this method fails.
The following example shows the usage of java.lang.reflect.Method.invoke(Object obj, Object... args) method.
package com.tutorialspoint;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodDemo {
   public static void main(String[] args) 
      throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

      Method[] methods = SampleClass.class.getMethods();
      SampleClass sampleObject = new SampleClass();
      methods[1].invoke(sampleObject, "data");
      System.out.println(methods[0].invoke(sampleObject));
   }
}

class SampleClass {
   private String sampleField;

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;
   } 
}
Let us compile and run the above program, this will produce the following result −
data

 Print
 Add Notes
 Bookmark this page