Tutorials Java Invoke Method Using Java Reflection

Invoke Method Using Java Reflection

Java provides reflection API to do introspection of an object and modify its behavior on the run time. The class Class in the Java API forms the basis to identify the object class and its internal structure of that particular Class. The below three functions are important to get the class behavior. In this articles we will see example invoke method using java reflection.

Example: Invoking method runtime using reflection

In mobile manufacturing company, the company wants to perform the quality check for each functionality of the product before it goes into market. The functionality of the product can be added or removed for each release.

For the above scenario consider a validator class which contains all the methods to check the quality of the product. A product class which contains describes the attributes of the product. Quality check class which is invoked by the client to check the product quality before it is checked. In this approach only validator class needs to change if any criteria will be added in future. The client code need not be changed.


public class Product {

	String productName;
	String category;
	String outgoingCallAbility;
	String ChargingCapability;

	public String getProductName() {
		return productName;

	public void setProductName(String productName) {
		this.productName = productName;

	public String getCategory() {
		return category;

	public void setCategory(String category) {
		this.category = category;

	public String getOutgoingCallAbility() {
		return outgoingCallAbility;

	public void setOutgoingCallAbility(String outgoingCallAbility) {
		this.outgoingCallAbility = outgoingCallAbility;

	public String getChargingCapability() {
		return ChargingCapability;

	public void setChargingCapability(String chargingCapability) {
		ChargingCapability = chargingCapability;



public class ProductValidator {

	public boolean checkChargingCapability(Product p)
			return true;
			return false;
	public boolean CheckOutgoingCallAbility(Product p)
		if(p.getOutgoingCallAbility()!=null && p.getOutgoingCallAbility().equalsIgnoreCase("Y"))
			return true;
			return false;


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

public class QualityCheckClient {

	 * @param args
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {

		String dispatch = "proceed";
		Product p = new Product();
		ProductValidator validate = (ProductValidator) Class.forName(
		Method[] methods = Class.forName("reflection.ProductValidator")
		for (Method m : methods) {
			boolean test = (Boolean) m.invoke(validate, p);
			if (test) {
				System.out.println("Successfully Executed " + m.getName() + " method");
			} else {
				System.out.println("Execution failed for" + m.getName());
				dispatch = "fail";

		if ("fail".equalsIgnoreCase(dispatch)) {
			System.out.println("Product cant be proceed for dispatching");



Successfully Executed the method-->checkChargingCapability
Execution failed for-->CheckOutgoingCallAbility
Product can’t be proceed for dispatching
Inline Feedbacks
View all comments
Previous Post

Bamboo Continuous Integration for Android

Next Post

MBOX to Outlook Converter Software Product Review

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
Would love your thoughts, please comment.x