mercredi 25 mars 2015

Generic way to instantiate an object name and set its values

I was wondering if there was a way that allows you to create an object using a string like



Class<?> classObject = Class.forName("Client");
Object object = classObject.getConstructor().newInstance();


but be able to set the values of the variables to the name of the field


so:



Field[] fields = object.getClass().getDeclaredFields();
for (int i=0; i< fields.length; i++)
{
object.???????? = fields[i].getName();
}


so I would have object.name = "name", object.address = "address" etc


I'm not sure how to use the properties of the instance I have created.


Does anyone know a way to do this?


Thanks!






Aucun commentaire:

Enregistrer un commentaire