Casting to a type only known at run time

by Patricia Shanaha » Thu, 18 Nov 1999 03:00:00 GMT



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




Casting to a type only known at run time

by Mitchell Ratishe » Fri, 19 Nov 1999 03:00:00 GMT



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



Casting to a type only known at run time

by Steve Chape » Fri, 19 Nov 1999 03:00:00 GMT




> 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;

I know you told us not to ask, but why do you want to do this? The only
thing you could possibly cast to is a String. Even if String weren't final
and you had a subclass, what possible good would it do to cast the reference
to that subclass? In effect, the reference would still be cast to a String
by assigning it to a String reference.
Let's say you're proposing to add this "feature" to Java. What would it give
us that we don't already have?



Casting to a type only known at run time

by Buck » Fri, 19 Nov 1999 03:00:00 GMT






> > 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;

Sort of like they way the dynamic cast works in C++? I don't think they have
that in Java.