In a String I've got the type of a class, in another one the value. I want to create an instance of the class with the value...
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alexia_Schumacker
Posted On:   Tuesday, November 19, 2002 12:09 AM

Hi,



I've got two String :

String one "returnType.getName()" = type of a class (for example: java.sql.Timestamp)

String two = "22102002" the date



with this I create an instance :

instance = Class.forName(returnType.getName()).newInstance();

But my problem is to assign the String two to this instance...


Thank you for your help

Alexia

Re: In a String I've got the type of a class, in another one the value. I want to create an instance of the class with the value...

Posted By:   Colin_Dunn  
Posted On:   Tuesday, November 19, 2002 12:54 AM



Try this, should be easy to adapt to your code:



Class myClass=Class.forName(returnType.getName());

Class[] paramClassArray=new Class[]{String.class};

Constructor constructor=myClass.getConstructor(paramClassArray);

Object object=constructor.newInstance(new Object[]{"22102002"});
About | Sitemap | Contact