0

Assuming that there is no special logic in a field set method, What would be the best practice to set field's value using reflections? would it be

  field.set(obj, value);

or

  method = obj.getClass().getMethod("setSomething", parameter.class);
  method.invoke(obj, argument);

1 Answers1

0

Well, it depends. There are many good reasons to use setter methods and if they are available, you should also use them via reflection. But you must be sure that the setter method exists otherwise an exception will occur. And if you want to prevent any (explicit unwanted) side effect you should also avoid using a setter method.

Milgo
  • 2,617
  • 4
  • 22
  • 37