Hi!
You can't do that.
You are going to cast an object to something that you
don't know what it is going to be.
Then what are you going to do with the object after that?
Do you know any method to call or what?
The whole code after that should be also "dynamic".
This sounds like you wan't to do a dynamic introspection
of the class. You should look at the java.lang.reflect
package. Anyway you should think twice before going into
the introspection(reflection) of classes and methods.
Is there any other(more lightweight) ways to solve the problem.
In other hand if you know that the object you have implements
some known interface e.g. Runnable, you can cast the object to
a Runnable and call the method run(), but nothing else.
Runnable r = (Runnable) o;
r.run();
NOTE! The interface can by any interface that is known at compile
time and the object is really implementing it.
This idea is that you don't have to know all the details about the
object, just that it implements the certain agreed interface.
regards, Jari
> -----Original Message-----
> From: EXT SABYASACHI S GUPTA
> [mailto:[EMAIL PROTECTED]]
> Sent: Friday, November 05, 1999 9:22 AM
> To: [EMAIL PROTECTED]
> Subject: Re. about Class class
>
>
>
> Yup I figured that out ...
> but how do I do a typecast when I have the class name in a String
> variable during runtime. (I do NOT know the class name to typecast).
>
> On Thu, 4 Nov 1999, Nathan Meyers wrote:
>
> > Nathan Meyers wrote:
> > > SABYASACHI S GUPTA wrote:
> > > >
> > > > public class saby {
> > > >
> > > > public static void main(String args[])
> > > > {
> > > >
> > > > String s = "myclass";
> > > > Class c = new Class();
> > > > try {
> > > >
> > > > c = Class.forName(s);
> > > >
> > > > Object b = c.newInstance();
> > > > b.method(); // I know this is an
> error..so what to do?
> > >
> > > You need to use the reflection API. Use b.getMethod() to
> get a handle
> > > (of type Method) for the method, then use Method.invoke()
> to run that
> > > method.
> >
> > Please ignore previous dumb advice :-). You would use the Reflection
> > API on c (the Class instance). You need a typecast to use b
> (the myclass
> > instance).
> >
> > Nathan
> >
>
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
>
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]