SABYASACHI S GUPTA wrote:
>
> I dont know this might be something very simple...
> so please excuse me.
>
> I have the following small code.
>
> public class saby {
>
> public static void main(String args[])
> {
>
> String s = "myclass";
> Class c = new Class();
One thing... you don't need the "new Class();". You don't use c until
after you've already changed its value to something else (3 lines down).
> 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.
Nathan
> }
>
> catch (Exception e) {
> }
> }
> }
>
> // myclass.java
>
> public class myclass {
>
> public void method()
> {
> System.out.println("hello world");
> }
> }
>
> Now I get the compilation error that method is not there in Object.
> So now if I want to call the method of the newly created instance
> what should I do...
>
> ----------------------------------------------------------------------
> 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]