constructor problem !!!

Hi !

At some point inside a class, I have name of the constructor of another class and the arguments to be passed to it in the STRING format. I need this information to invoke this constructor of other class. How can I do that?

For example, I m in the constructor of the class named "Carrier". Inside this Carrier's constructor, I have a string named "Service" and another string named "Vai". Service is another class. I want to invoke the constructor of Service class [ which is Service(String str ) ] with the arguments as a string "Vai".

Can ne 1 help me with this matter?

Thanks!

-Vaibhav.

[639 byte] By [v_sanas] at [2007-9-26 21:12:29]
# 1

The Class class allows you to do this

but className needs to be fully qualified (include the package name)

so if your Service class is in package "util", the following should work:

Class class = Class.forName("util.Service");

Constructors[] cons = class.getDeclaredConstructors();

//not sure if you need some code here to figure out which constructor you want

String[] strArray = new String[] {"Vai"};

Service service = (Service)constructor[0].newInstance(strArray);

monkeymoo at 2007-7-3 20:39:44 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...