It's about understanding the operator instanceof and the Class class.
Type Comparison Operator instanceof:
At run time, the result of the instanceof operator is true if the
value of the RelationalExpression is not null and the reference could
be cast to the ReferenceType without raising a ClassCastException.
Otherwise the result is false.
This means that your code if (part instanceof c) ... will always return false, as part can never be cast to Class.
If you have a look at Class.inInstance(Object obj), you find that
This method is the dynamic equivalent of the Java language instanceof operator.
In other words:
- If you know the checked type in compile-time, you use
instanceof.
- If you do not know the checked type in compile-time, but in run-time (so you have it stored in a variable of the
Class type), you use Class.inInstance(Object obj).