i have a problem as : MyFirstClass.java
public class MyFirstClass{ String strParam; MySecondClass secClass; //getters and setters }
MySecondClass.java
public class MySecondClass{ String p1Param; String p2Param; String p3Param; String p4Param; //setters and getters }
TestClass.java
public void doProcessing(MyFirstClass pObj){ for(int i=0;i<3;i++){ System.out.println() ; //###question is here }
I want to use java reflection and call the pObj.getP**Param() methods in for loop by providing the parameters to g
Method m= cls.getMethod("getP"+(i+1)+"Param"); pObj.getSecClass.[ invokeMethod m ]
How it can be made possible. I don’t want to use array of p*Params.
Thanks in advance.
Advertisement
Answer
Are you sure this is what you need? For me, your code seems to be a typical example of the current trend of abusing reflection for every task. Why don’t you use arrays or maps to store the properties p[0-9]+Param
.
Arrays were invented to do exactly that. They provide a range of variables, indexed by numbers.