lundi 22 octobre 2018

Java - set field given field's name in String

I have field's String name and I would like to set that field with other particular value.

I know that we can do that through reflection like so:

DemoClass demo = new DemoClass();
Class demoClass = demo.getClass();

Field field = demoClass.getField("field1");
field.set(demo, "Hello");

Is there any other approach to NOT use reflection? The caveat is that we can't change class itself in anyway.





Aucun commentaire:

Enregistrer un commentaire