• Post Reply Bookmark Topic Watch Topic
  • New Topic

passing method names as string  RSS feed

 
prasanna kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

i have a situation where I'm passing method name as a string. i need to call this method on a object.

let me make myself clear..

lets say there is a object bankdetails. it has like 100 methods method1(), method2(),...and so on
i have a string object which gets method names like "method1" or "method2" or..
i need to check this string value and call bankdetails.method1 or bankdetails.method2 or... so on based on this string's value.
one way of doing this would be to use multiple if then else statements to see the values and call the appropriate method. in this case if the object has 100 methods then i need to write 100 statements to check for this...
like (if methodname == "method1" then call bankdetails.method1 else if methodname == "method2" then call bankdetails.method2 ..)

i surely feel there should be another way of doing this..

thanks in advance
arun
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use the reflection API for this.
Here is a very simple example to execute your methods by name.
Here the bankDetails methods to be executed have no arguments and return nothing.

Regards
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!