Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Java Reflection

Posted by Pankil Patel on May 27, 2010

How to call method of specific class by Java Reflection?

Employee emp = new Employee();

String forMethodName = “Name”;
String forMethodCode = “Code”;
String forMethodRole = “Role”;

Method m1 = emp.getClass().getDeclaredMethod(“get” +forMethodName);
Method m2 = emp.getClass().getDeclaredMethod(“get” +forMethodCode );
Method m3 = emp.getClass().getDeclaredMethod(“get” +forMethodRole );

Object o1 = m1.invoke(emp);
Object o2 = m2.invoke(emp);
Object o3 = m3.invoke(emp);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: