I don't think it is possible to do, and would not have any known use.
The only effect of casting is to change the type of a reference, and to
do anything useful with that you need to know the type you are casting
to as you are writing the rest of the expression. In particular, the
reflection API uses Object references to do things like calling methods
that are not known until run time.
> Is it possible to cast to a type which is only known at run time? Don't
> ask why I want to do this, its a long story and I'm mostly curious and
> trying to learn more. Basically, I want to do this:
> String className="????";
> String s1="junk";
> Object o1=s1;
> String s2= ( Class.forName(className) )o1;
> So the cast would work if "????" was replaced by "java.lang.String", and
> generate a ClassCastException otherwise. However, this does not compile.
> Can I put anything inside the casting parantheses other than a hardcoded
> class name? Or have I just learned why Java is considered a strongly
> typed language?
> --
> Mitchell Ratisher
> Silicon Valley Resident
> "Winners are losers who got up and gave it just one more try"
> -Dennis DeYoung