I have a class in Categories.java with variable definitions, which I want to read with Reflection:
and I want to read the value of the String with the following program called Reflect.java :
and this is the error message:
I am using this type conversion because there is no getString() or getStr() method in java.lang.reflect.Field - what else can I use? I've been googling for this, but found no solution now that some hours have passed.
Thank you for helping me with this!
The original description of the problem was too short. In the original sourcecode (not the example I posted here) I only had an Object available, and not the class Categories itself. Besides that, I also passed the wrong object instance to getField, not really knowing that it had to be an instance of the class Categories.
I figured out how it works, using an Object instead of Categories itself:
The output is:
Jesper, thank you for the good, short answer and the tips regarding the initialization of my variables
Don't do that, please. You should get the String class literal and compare directly:
Likewise for Integer, Long, etc. Remember, you can get a class literal for each class by just saying X.class, with X being the class name. You can even do that for arrays, for instance String.class is the class for String arrays, and String.class is the class for arrays of arrays of Strings.