Reputation:
I'm getting this error:
incomparable types: Class and String where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?
Object object;
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i].getType() == String) { //On this line the compiler error is displayed
//Can't get in here
}
}
I thought I was comparing two of the same classes in that if
statement?
Upvotes: 1
Views: 1125
Reputation: 94429
getType returns a class so you must compare it with a Class
. To get the class for String
you must use String.class
.
if (fields[i].getType() == String.class) {
//Now you can get in here!
}
Upvotes: 3