Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically calling method of another class

Status
Not open for further replies.

cgreynes

Programmer
Feb 28, 2003
29
US
Hi,

How do I call a method of another class based on the class name returned from database?

for example

- public class ActParent {}

- public class ActChild1 extends ActParent {
public void execute(Bean bean){
}
}

- public class ActChild2 extends ActParent {
public void execute(Bean bean){
}
}

- public class WebDriver {
protected void process() {
String className = getClass(request.getParameter("ACT")); //get class name from database based on query string
//process execute() function using String className ??????
}
}

I heard about a third party eval() function which works the same as the javascript eval() which executes the the String at runtime. I am not sure if it will work or it is the best option. Does anyone know offhand the best way to do it?

Another question. Is it recommended to use two Hash Map as instance variables in a bean? To retrieve a value from a bean is bean.getValue(ScopeString, KeyString).

Thanks
clark

 
I don't think I understand the question: where is the object you want to call the method? Do you want to instatiate ir or it is already instantiated?

Reflection (what prosper showed you) ccan be the sollution, but that often hides a design problem that can arise later.

Cheers,
Dian

 
actually I want to mimic the WebDriver class from a B2B web application. the function of the WebDriver is to call the action class corresponding to the querystring "ACT" given. Using this querystring value, a class name can be retrieved from the database. All I need is to call the execute function of the class based on the class name returned from database.

I want to instantiate the object using the class returned. My problem is that I can only create the object using ActParent but cannot set the object to the class name returned from database.

Hope I explained well. I am sorry if not. I am new still to java and still learning the terminologies.

I will try prosper's code for now. What design problem will arise?
 
Thanks Dian and prosper:D I got more info when I searched "reflection".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top