mardi 3 mai 2016

Set variable in Constructor.newInstance while using Reflection

I'm still fairly new to Java and I'm sorry if I asked a bad question. I've been encountered a problem here. I just learned about using Reflection method to grant access of private constructor for main class outside the object class. Here are my test program code:

Constructor<Fan> constructor = Fan.class.getDeclaredConstructor(new Class[0]);
constructor.setAccessible(true);
Fan obj = constructor.newInstance(new Object[0]);
Fan obj1 = constructor.newInstance(new Object[0]);
System.out.println(obj);
System.out.println(obj1);

"Fan" is my Class.

For the obj, it will output my default data which is normal. But for the obj1, if I want to change it's variable, how should I declare it? Should I do it in test class or object class?





Aucun commentaire:

Enregistrer un commentaire