How do I call a method using reflection?
Author: Deron Eriksson
Description: This Java tutorial describes how to use invoke a method using reflection.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4


Page:    1 2 >

In JavaSW, it is possible to invoke a method on an object or class using the reflection API. This is demonstrated in the ClassMethodTest class below. A 'Testing' object is instantiated, and its class object is obtained via a call to getClass() on the object.

Next, a Method object is obtained for the getString1() method of Testing by calling the getMethod() method of the Testing Class object that we have. The first parameter is the getMethod() call is the method name, and the second call is an array of Class objects representing the parameters of the getString1() method. Since getString1() has no parameters, this array is empty.

We invoke the getString1() method on our Testing object by calling invoke() on the Method object. For the first parameter of invoke() we pass in the Testing object on which we'd like to call the method, and the second parameter of invoke() is an array of Objects that represent the parameter values that we'd like to pass to the method when we invoke it. We cast the returned value of the invocation to a String and we display the result to the console.

Following this, we make a similar call to setString1() and another call to getString1().

ClassMethodTest.java

package test;

import java.lang.reflect.Method;

public class ClassMethodTest {

	public static void main(String args[]) throws Exception {

		Testing t = new Testing("val1", false);
		Class tClass = t.getClass();

		Method gs1Method = tClass.getMethod("getString1", new Class[] {});
		String str1 = (String) gs1Method.invoke(t, new Object[] {});
		System.out.println("getString1 returned: " + str1);

		Method ss1Method = tClass.getMethod("setString1", new Class[] { String.class });
		System.out.println("calling setString1 with 'val2'");
		ss1Method.invoke(t, new Object[] { "val2" });

		str1 = (String) gs1Method.invoke(t, new Object[] {});
		System.out.println("getString1 returned: " + str1);

	}
}

The Testing class is shown below.

Testing.java

package test;

public class Testing {

	private String string1;
	private boolean boolean1;
	private String privateDude;
	private String protectedDude;

	public Testing() {
	}

	public Testing(String string1, boolean boolean1) {
		this.string1 = string1;
		this.boolean1 = boolean1;
	}

	private String getPrivateDude() {
		return privateDude;
	}

	private void setPrivateDude(String privateDude) {
		this.privateDude = privateDude;
	}

	protected String getProtectedDude() {
		return protectedDude;
	}

	protected void setProtectedDude(String protectedDude) {
		this.protectedDude = protectedDude;
	}

	public boolean isBoolean1() {
		return boolean1;
	}

	public void setBoolean1(boolean boolean1) {
		this.boolean1 = boolean1;
	}

	public String getString1() {
		return string1;
	}

	public void setString1(String string1) {
		this.string1 = string1;
	}

}

(Continued on page 2)

Page:    1 2 >