John Smith John Smith - 1 month ago 7
Java Question

How do I cast if the "to-cast" class is a string?

This form of cast is okay:

((CastTo)this).getStates();


but I have:

Class<? extends CastTo> aClass = this.getClass();
((whatToWriteHere"CastTo"DoesntWork)this).getStates();


How would I cast the result? What would I use to cast with a
Class
object?

Answer
Class<? extends CastTo> aClass = this.getClass();
aClass.cast(this).getStates();
Comments