This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes passing method names as string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "passing method names as string" Watch "passing method names as string" New topic
Author

passing method names as string

prasanna kumar
Greenhorn

Joined: Jun 27, 2004
Posts: 12
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

Joined: Mar 03, 2004
Posts: 101
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: passing method names as string
 
Similar Threads
Continuation of basic principles
Overriden Method Query
exception declaration
Anonymous inner class
Null values