I have to compare a Class object against a list of pre-defined classes.
Is it safe to use == or should I use equals?
if (klass == KlassA.class) {
} else if (klass == KlassB.class) {
} else if (klass == KlassC.class) {
} else {
}
Note: I cannot use instanceof, I don’t have an object, I just have the Class object. I (mis)use it like an enum in this situation!
java.lang.Classdoes not override theequalsmethod fromjava.lang.Object, which is implemented like this:So
a == bis the same asa.equals(b)(except ifais null).