Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change the method names dynamically using for Loop ?

 
Ramesh Jothimani
Ranch Hand
Posts: 70
Android Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having methods called setStAmount1(),setStAmount2() ...... setStAmount10() . I am not sure how many setStAmount() methods I will call . I will call depending upon the values I get in the bean. How to make that method name dynamic ? I need to append setStAmount+value of i to the method name by looping it . Please suggest a solution.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Jothimani wrote:Hi,

I am having methods called setStAmount1(),setStAmount2() ...... setStAmount10() . I am not sure how many setStAmount() methods I will call .


It's hard to say for sure without real requirements and use cases, but that looks like bad design.

I will call depending upon the values I get in the bean. How to make that method name dynamic ? I need to append setStAmount+value of i to the method name by looping it . Please suggest a solution.


If you're dead set on pursuing this approach, you can use Java's reflection mechanism. Google for java reflection tutorial, look at java.lang.Class.getDeclareMethod() and getDeclaredMethods(), and java.lang.reflect.Method.invoke(). However, I suggest you first look into using an array, List, or Map for this.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:If you're dead set on pursuing this approach...

Which you should not be! Use a better design.
 
RatiKanta pal
Ranch Hand
Posts: 88
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramesh,



Using variable it will be problem you can mention your functions in cases.

Please go through the part i have commented .
 
Ramesh Jothimani
Ranch Hand
Posts: 70
Android Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to every one for your replies . A nice idea RatiKanta pal . I haven't gone for that design. I have asked it for the Purpose of passing values to Jasper Reports but I have found that arrays can directly be passed and processed . I have implemented it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RatiKanta pal wrote:


Are you sure that will work? I haven't tried compiling it, but I can't see how that would work. Where is the method getx12() defined?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RatiKanta pal wrote:
Please go through the part i have commented .


You mean as a demonstration of what doesn't work, right?
 
B Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are dead set on doing this you want to look at the reflections package (java.lang.reflect) which will allow you to call methods by looking them up by name, e.g.



Having said that, you almost certainly have the wrong design here and what you really need is a method like:

 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramesh Jothimani wrote:I am not sure how many setStAmount() methods I will call .

Why Ramesh? Can you describe the requirement that causes you to believe that?

Another way I can ask that question is: What is the problem you are trying to solve that causes you to see the need for multiple methods?


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic