0

Possible Duplicate:
How do I invoke a java method when given the method name as a string?

I have a few methods myMethod0() ... myMethodN(), where N is a predetermined number. How can I execute all methods in sequence? Meaning, how can I do this, without explicitly writing all those methods calls? I guess a `for-loop would be nice? But how to do this elegantly?

Community
  • 1
  • 1
jottr
  • 3,071
  • 2
  • 23
  • 33
  • This is really just a special case of: [How do I invoke a java method when given the method name as a string?](http://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string) – Mark Elliot Nov 06 '10 at 22:41
  • some times we over engineer. I think a simple for loop will do just fine rather than doing it with reflection API. – zengr Nov 06 '10 at 22:46
  • @zengr: how would you do it using a for loop _without_ using reflection? – Matt Ball Nov 06 '10 at 22:59
  • 1
    @zengr: the point is the OP needs to form method names *dynamically*, the only way to do this is by creating strings and using the reflection API... – Mark Elliot Nov 06 '10 at 22:59
  • oh, maybe I did not ask my question correctly: N is a predetermined number. So all I want to do is iterate over the functions. – jottr Nov 06 '10 at 23:01
  • @elementz, if you don't call each function symbolically that means you need to call it some other way -- there's only one other way, and that's through the reflection API. – Mark Elliot Nov 06 '10 at 23:08
  • @Matt my bad, I misunderstood the question. – zengr Nov 06 '10 at 23:53

0 Answers0