How to dynamically invoke method using Reflection

As most of us know, java.lang.reflect package has classes and interfaces related to Java Reflection.Java provides Reflection to assess any Java Class or object dynamically, during run time. This Reflection feature also provides mechanism to invoke method of any object dynamically during run time.

Below is an example, on how to invoke any method of an object, dynamically. This feature may be used to use functionality of jar file dynamically loaded, during run time.

import java.lang.reflect.*;

class Abcd
{
	public Abcd()
	{
		System.out.println("This is Constructor");
	}

	public void met1()
	{
		System.out.println("This is met1()");
	}

	public void met2(String str)
	{
		System.out.println("This is met2(String) "+str);
	}

	public void met3(String str1,String str2)
	{
		System.out.println("This is met3(String, String) "+str1+" "+str2);
	}
}

public class ReflectionDemo {
public static void main(String args[])
{
	try
	{
	//no parameters
	Class no_params[] = {};

	//get the Class instance of Abcd
	Class c = Class.forName("Abcd");

	//get the Constructor instance
	Constructor cons = c.getConstructor(no_params);

	//create object, which is required for invoke() method
	Object obj = cons.newInstance();

	Method method1 = c.getDeclaredMethod("met1",no_params);

	//Now, invoke method dynamically
	method1.invoke((Abcd)obj);

	//one String parameter
	Class str_params[] = {String.class};
	Method method2 = c.getDeclaredMethod("met2", str_params);

	//Now, invoke method dynamically
	method2.invoke((Abcd)obj, "One Param");

	//two String parameters
	Class str_str_params[] = {String.class, String.class};
	Method method3 = c.getDeclaredMethod("met3", str_str_params);

	//Now, invoke method dynamically
	method3.invoke((Abcd)obj, "First Param", "Second Param");
	}catch(ClassNotFoundException cnf)
	{
		cnf.printStackTrace();
	}
	catch(NoSuchMethodException nsm)
	{
		nsm.printStackTrace();
	}
	catch(Exception ec)
	{
		ec.printStackTrace();
	}
	}
}

You may also like to read:
Java Reflection Simple Example

Java Reflection example

Reflection is capability of software to analyse itself. java.lang.reflect package has interfaces and classes to get any detail of an object or class, during runtime. Below is java program, showing java reflection.

Below is English meaning of word, Reflection
the throwing back by a surface of sound, light, heat, etc.

package com.java652;
import java.lang.reflect.*;

class Simple{
public void xyz()
{

}

public int met()
{
    return 0;
}
}
interface My{}

class Javalang{
 public static void main(String args[]){
  try{
   Class c=Class.forName("com.java652.Simple");
   System.out.println("is Simple an interface?"+c.isInterface());

   Method mets[] = c.getMethods();
   for(Method met:mets)
   {
       System.out.println(met.getName());
   }

   Class c2=Class.forName("com.java652.My");
   System.out.println("is My an interface?"+c2.isInterface());

  }catch(Exception e){System.out.println(e);}

 }
}

Download Java Reflection Example, Eclipse Project

You may also like to read:
Dynamically invoke method using reflection