• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Method Call using method name as string

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Say i have a class A with 10 methods in a class.
and one of the methods being
public void doWork(){
System.out.println("test"};
}
i have another class B in which i have created an object of the A class .
now in the main method of class B i take an argument "doWork" which is a string (obvioulsy)
is it possible for me to call the methods on the class A based on the string variable which is going to be exactly the name of the method .??
how do i do this ..
hey i dont want to use any if or any other conditional blocks ..
something like
//String s = "doWork"
//s"();"
Thanks Guys ..
Sunil
 
sunil mirani
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys , thanx i got it . a friend of mine helped me .. following is a sample code for u guys ..

bye,
sunil
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good source of information on reflection is Sun's Reflection API Tutorial.
 
reply
    Bookmark Topic Watch Topic
  • New Topic