aspose file tools*
The moose likes Java in General and the fly likes How to change the method names dynamically using for Loop ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to change the method names dynamically using for Loop ?" Watch "How to change the method names dynamically using for Loop ?" New topic
Author

How to change the method names dynamically using for Loop ?

Ramesh Jothimani
Ranch Hand

Joined: May 05, 2011
Posts: 70

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 10, 2002
Posts: 61612
    
  67

Jeff Verdegan wrote:If you're dead set on pursuing this approach...

Which you should not be! Use a better design.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
RatiKanta pal
Ranch Hand

Joined: Nov 13, 2011
Posts: 68

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

Joined: May 05, 2011
Posts: 70

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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2012
Posts: 6
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

Joined: Oct 10, 2012
Posts: 86
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?


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to change the method names dynamically using for Loop ?