[java] ClassLoader

Started by
5 comments, last by Redferne 22 years, 1 month ago
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()
Advertisement
Tester t = (Tester)o;
t.doSomething();


Slightly shrimpy smell == unsafe breadbin
Slightly shrimpy smell == unsafe breadbin
The class is loaded at runtime, so the name of the class can''t be hardcoded (because it could be called anything)
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.
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
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)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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

This topic is closed to new replies.

Advertisement