SABYASACHI S GUPTA wrote:
>
> 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).
Ah, I guess I really did understand your original question and gave
decent advice :-). If you're trying to learn your way around an unknown
class, the Reflection API is the way to go.
Since your questions relate to Java development and not to Java on
Linux, you may find a better source of help in the comp.lang.java.help
newsgroup.
Nathan
> 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]