java - How do I compare classes using reflection? -
I am trying to determine the square type of the square and then make something specific. For example, if the class is double , Then use the double specific method.
I am trying to use
if (f.getClass () == Double.class) However , I am getting a compiler error:
"Incompatible Operant Type class
What is the proper way to do this?
Edit: To be more clear
f is the type of field obtained from a reflection in the loop
(Field F: instance.getDeclaredFields ())
Interest The error message (I did not know that the '==' operator would check them). But on the basis of this, I suspect that your comparison is wrong: you are trying to see that the field class (theoretically Super-class, but only in theory - the field is final) is similar to double class, which can not be it.
So: the comparison should work, if you give it the correct argument I suspect you want to do this May include:
If (f.getType () == double. Class)
Instead it should work, given that the double is a final square. Otherwise "isAssignableFrom" would be more appropriate.
Comments
Post a Comment