How to access the field of another class Test.characterAnimation using a string?

Closed due to the fact that the question is not clear to the participants of Akina , user194374, Streletz , post_zeew , Mikhail Vaysman 28 Feb '17 at 0:42 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Perhaps you need a reflection - YuriySPb

1 answer 1

Using reflection:

String fieldName = "characterAnimation"; try { Field field = Test.class.getDeclaredField(fieldName); Test test = new Test(); Object value = field.get(test); System.out.println(value); } catch (Exception ignored) { } 

If the field is static, then instead of test you can pass null .

If the field is not available in the class in which this code is used, it can help:

 field.setAccessible(true);