> I have the following small code.
> 
> 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?
>                 }
> 
>                 catch (Exception e) {
>                 }
>         }
> }

You need to do a typecast:
         Object b = c.newInstance();
        ( (myclass)b).method();

or 

         myclass b = (myclass) (c.newInstance());
         b.method();

newinstance() always returns an object of type Object which 
(naturally) does not know anything about the myclass members.

If youŽd like to use a more generic approach, use either interfaces 
which are implemented by the myclass instances or use a parent 
myclass class from which the instantiated classes have to be 
derived.

BTW, the Class c = new Class(); statement is obsolete as 
Class.forName(s) already returns a newly created Class object.

Oliver

___________________________________________________
Oliver Fels                    | e-mail:         
IQena GmbH                     | [EMAIL PROTECTED]
Team Manager JAVA-/IT-Security | http://www.iqena.com
Friedrichshafen, Germany       | 
---------------------------------------------------


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to