Archived

This topic is now archived and is closed to further replies.

Redferne

[java] ClassLoader

Recommended Posts

I''ve been experimenting with the ClassLoader, and everything seems to work up to the point where I have a Class object of my newly loaded .class file. This is the class I''m loading; public class Tester { public void doSomething() { System.out.println("w00t"); } } If newclass is the Tester class I just loaded, how do I call doSomething() from ''o''? Object o = newclass.newInstance(); //o.doSomething()

Share this post


Link to post
Share on other sites
Look classes at package java.lang.reflect

In particular java.lang.reflect.Method

Everybody of us who like code in C++ would like these were part of it.

Share this post


Link to post
Share on other sites
Hey Redferme

Edit your profile,
click View Your Profile anchor,
look at the other thread you have participated.

There is the same response I tell you in my previous reply

Share this post


Link to post
Share on other sites
quote:
Original post by Redferne
The class is loaded at runtime, so the name of the class can''t be hardcoded (because it could be called anything)

Since the name of the method you are calling is obviously hardcoded, have the class you load implement an interface with the appropriate method and cast to that.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
thanks Pepe, I had been trying to cast it to a superclass that defined the same method, but I couldn't get it to work. Your suggestion works great however, thanks again

Edited by - Redferne on February 23, 2002 4:09:41 AM

Share this post


Link to post
Share on other sites