Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a static method of another class from an applet

 
Marilyn de Queiroz
Sheriff
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to call a static method of another class from an applet where the directory structure looks like:

path/.../applets/MyApplet
path/MyClass

So far I have:

MyClass myClass = (MyClass)myApplet.getClass().getClassLoader().getParent().loadClass("path.MyClass").newInstance() ;

myClass.staticMethod();

The problem is that this won't work because MyClass has only static methods and has a private constructor. Therefore I won't be able to create a newInstance of it.

How can I use a static method from this class without creating an instance of it?
[ July 23, 2002: Message edited by: Marilyn de Queiroz ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an interesting problem. The semantics of calling a static method require that you have a reference to the Class object in order to invoke the static method. In your source code, it is legal but bad style to use an instance reference to invoke the static method. But, this is usually handled for you by the compiler, ie, when you compile and (and implicitly link) static references are resolved at compile time, ie, they are non-virtual function calls. This implies that you can't invoke non-virtual functions dynamically.
I can't find any methods in Class to get a list of static methods nor invoke them, so you may not actually be able to do what you are trying to do. I'll keep looking though.
 
Marilyn de Queiroz
Sheriff
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Rob, I normally don't instantiate a class to use a static method, but without the instantiation at the end I was unable to compile the applet. If I just use

(MyClass)staticMethod() ;

I get the one from my classpath rather than the one from the server the applet is being downloaded from.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is you still have to use reflection to invoke the static method, as you would with an instance method. The only difference really is that in the case of calling the static method, you don't need to have an instance of the class whos method you are calling. Here is some example code to show how to do it.
Assumptions:
I have a directory path
C:\NetBeansDev\CattleDrive\refTest
in this directory I have a compiled java file named Reflector.class. The source files are in the refTest package. The source code for this compiled class file is:

The above is the class to be loaded dynamically. The program to load it and execute the print() method follows:


Hope this helps!
 
Marilyn de Queiroz
Sheriff
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rob. I'll try that out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic