Altough RTTI (Real Time Type Identification) is considered a code smell by some individuals, there are two alternatives, one is to use the instanceof
operator:
if(value instanceof MyClass)
On the other hand, you can use a full fledged method from the Class
class, that given two objects, you can determine if they belong to the same hierarchy (much more powerful than instanceof
IMO):
if(value.getClass().isAsignableFrom(getClass()))
This second approach determines if value is the very same class or a superclass/superinterface of the current class (the this
) at execution time given any kind of object. This is where isAsignableFrom
excels, since with instanceof
you need to know the reference type at compile time.