0

How to dynamically call methods of Java Bean.

I have the following methods in a class of Java:

public class Bean {
   public String column1val;
   public String column2val;
   public String column2val;

} 

I need to access these methods from for loop like the below:

for(int i=0;i<2;i++) { 
    String s = Bean.get column+i+val;
}


How can we achieve this scenario ?

user1276092
  • 523
  • 3
  • 8
  • 30

8 Answers8

2

Hi you can use Java Reflections for this purpose...

   //Obtain the Class instance
    Class beanClass = Bean.class;

    //Get the methods
    Method[] methods = beanClass.getMethods();

    //Loop through the methods and print out their names
    for (Method method : methods) {
        System.out.println(method.getName());
    }
Srinivas B
  • 1,821
  • 4
  • 17
  • 35
2

Sample Code.

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

public class Bean {
    public String getMethod1(String a) {
        return a;
    }

    public String getMethod2(String a) {
        return a;
    }

    public static void main(String[] args) throws SecurityException,
            NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Bean bean = new Bean();
        for (int i = 1; i <= 2; i++) {
            Method method = Bean.class.getMethod("getMethod" + i, String.class);
            System.out.println(method.invoke(bean, "Simple"));
        }

    }
}
1

You can't as Java needs the pre-qualified method names to executes. Its not a String so you can append it.

This question helps you for your better understanding Array of function pointers in Java

Or You can use Java Reflection as Other Suggest..

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
1

try as:

Bean beanobj=new Bean();
for(int i=0;i<2;i++) { 

   Method method= beanobj.getClass().getMethod("getMethod"+
                                   String.valueOf(i), new Class[]{});
    String s=(String)method.invoke(beanobj,"aaa");

}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You have use Reflection:

              MyBean m = new MyBean();
          Method[] methods =  m.getClass().getMethods();
          for(Method methid: methods){
              methid.invoke(m, "abc");
          }
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

You can try this:

Bean b = new Bean();
for(int i=0;i<2;i++) { 
    String s = (String) Bean.getClass().getMethod("getMethod" + i, String.class).invoke(b, "aaa");
}
Waqas Ilyas
  • 3,116
  • 1
  • 17
  • 27
0

You can use reflection to get the list of methods and call them in a loop, if that's what is your requirement. As rightly said above you can not dynamically form the method names at run time.

Swagatika
  • 3,376
  • 6
  • 30
  • 39
0

Using reflection it can be possible - But it is not really good way to call method -

Class<Bean> class1 =Bean.class;
for(int i=1;i<=2;i++) { 
    Method s = class1.getMethod("getMethod"+i, String.class);
    Object invoke = s.invoke(new Bean(),"Str");
    System.out.println(invoke);
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103