Passing object during runtime as a constructor parameter to of a run time object
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Prasad_P
Posted On:   Friday, May 30, 2003 05:49 AM

How can i pass a run time object as a constructor parameter of a runtime object ? consider the below sequence HashMap actions = new HashMap(); HashMap models = new HashMap(); actions.put("TestOp","Testing"); // here TestOp is the key and Testing is Class Name. models.put("TestOp","TestingPart"); // here TestOp is the key and TestingPart is Class Name.and //Testing part extends Test which is an abstract class. Object objValue = actions.get("TestOp"); Object objValue1 = models.get("TestOp"); try { // I would like to create a statement below l   More>>

How can i pass a run time object as a constructor parameter of a runtime

object ?
consider the below sequence

HashMap actions = new HashMap();

HashMap models = new HashMap();

actions.put("TestOp","Testing");

// here TestOp is the key and Testing is Class Name.

models.put("TestOp","TestingPart");

// here TestOp is the key and TestingPart is Class Name.and

//Testing part extends Test which is an abstract class.
Object objValue = actions.get("TestOp");

Object objValue1 = models.get("TestOp");

try

{

// I would like to create a statement below like

// Test testObj = new Testing(new (TestingPart))


how can i do this ?

I presume doing

Test testObj = (Test)getClass().getClassLoader().loadClass(objValue.toString()).newInstance();

// will create an object of Testing but how can i pass an object of TestingPart ?
testObj.perform();

}

catch(Exception e)

{

e.printStackTrace();

}


Please help me with suggestions. Thanks in advance for any reply.

   <<Less

Re: Passing object during runtime as a constructor parameter to of a run time object

Posted By:   Bahman_Barzideh  
Posted On:   Friday, May 30, 2003 06:52 AM

Your question is not very clear, but I am guessing you want to
create an instance of an object from its class name and the
constructor you plan on using takes one or more parameters. If so,
you need to use reflection's Constructor class. Something
like the following:

Class myClass = Class.forName ("com.none.MyStuff");
Class parameterTypes [] = { Param1.class, Param2.class };
Constructor constructor = myClass.getConstructor (parameterTypes);
Object parameters [] = { param1, param2 };
Object myObject = constructor.newInstance (parameters);
MyStuff myStuff = (MyStuff) myObject;
About | Sitemap | Contact