Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

Call a static method on a Class object?

Thread view: 
Jimi Hullegård - 28 Sep 2005 20:59 GMT
Lets say I have a class MyClass, that is final, and has a private
constructor. So I can't instantiate it.
Now, if I want to call a static method myMethod("hello"), I simply do
MyClass.myMethod("hello"). This is all basic stuff.
But what if I want to pass MyClass to another method, and from there call
myMethod("hello")?

This is what I thought would be possible, but it isn't:

...
doTest(MyClass.getClass(), "hello");
...

public void doTest(Class<MyClass> mc, String message)
{
   mc.myMethod(message);
}

This results in:
java.lang.ClassCastException: java.lang.Class

Why doesn't this work? Is there a way to represent MyClass as a real class
(not a Class<MyClass> object), so I can call mc.myMethod(message) on it?

Is there no way to call a static method on a class object without using
reflection? If the class could be instantiated it would be easy to call the
method on that object, but even then it seems stupid to instantiate an
object just to call on a static method.

Regards
/Jimi
Oliver Wong - 28 Sep 2005 21:19 GMT
> Lets say I have a class MyClass, that is final, and has a private
> constructor. So I can't instantiate it.
[quoted text clipped - 25 lines]
> method on that object, but even then it seems stupid to instantiate an
> object just to call on a static method.

   Have you looked at the Command pattern? It sounds like you're
essentially trying to pass around methods as if they were objects, which is
what the Command pattern allows you to emulate in Java.

   http://en.wikipedia.org/wiki/Command_pattern

   - Oliver
Jimi Hullegård - 28 Sep 2005 21:41 GMT
>> Lets say I have a class MyClass, that is final, and has a private
>> constructor. So I can't instantiate it.
[quoted text clipped - 32 lines]
>
>    http://en.wikipedia.org/wiki/Command_pattern

Well, that is more or less what I already have done, or atleast tried to do.
The example above is greatly simplified, because I can't express the _real_
issue in a easy way, and I though there was an easy solution that would work
on the real issue aswell.
But I will try to describe the real issue (but still simpified):

I have a webserver (apache & tomcat) that has different action managers (for
login, sendMessage etc.), that all get loaded dynamically (the servlet reads
a config file, and loads the class files using a custom class loader) so I
easily can change some functionallity or add a new function withouth
reloading the webapplication. Now, I don't want to instantiate these action
managers, but instead call their static methods.

For instance, when I load the action managers, I want to set a connection
pool (a static class variable in the action manager). This is how I do it
now:

...
String storageClass = st.nextToken().trim();
Class<ActionManager> sam = loader.forceLoadClass(storageClass);
java.lang.reflect.Method setConnectionPool =
sam.getMethod("setConnectionPool", community.storage.ConnectionPool.class);
setConnectionPool.invoke(null, communityManager.getConnectionPool());

But I really would like to do something like this:

...
String storageClass = st.nextToken().trim();
(*??*) sam = loader.forceLoadClass(storageClass);
sam.setConnectionPool(communityManager.getConnectionPool());

I just though that there was something I could replace (*??*) with to get it
to work. Apperently, Class<ActionManager> doesn't work, even though the
class ActionManager has a static method setConnectionPool that takes a
ConnectionPool object).

/Jimi
John C. Bollinger - 29 Sep 2005 05:29 GMT
> I have a webserver (apache & tomcat) that has different action managers (for
> login, sendMessage etc.), that all get loaded dynamically (the servlet reads
> a config file, and loads the class files using a custom class loader) so I
> easily can change some functionallity or add a new function withouth
> reloading the webapplication. Now, I don't want to instantiate these action
> managers, but instead call their static methods.

And there is your underlying problem.

Why don't you want to instantiate the managers?  Why are static methods
an advantageous implementation approach?

You should not define or use static methods without a compelling reason
to do so.  On the most superficial level, this is because it will cause
you problems such as (but not limited to) the one you are having.  More
fundamentally, it is because static members break *object* orientation.

> For instance, when I load the action managers, I want to set a connection
> pool (a static class variable in the action manager). This is how I do it
[quoted text clipped - 13 lines]
> (*??*) sam = loader.forceLoadClass(storageClass);
> sam.setConnectionPool(communityManager.getConnectionPool());

What you /should/ want to do is this:

public interface StorageActionManager {
// ...
}

// ...

String storageClass = st.nextToken().trim();
StorageActionManager sam =
    loader.forceLoadClass(storageClass).newInstance();
sam.setConnectionPool(communityManager.getConnectionPool());

Since you are loading the class in question dynamically anyway, you gain
absolutely nothing (other than headaches) by trying to use static
methods instead of instance methods.  My example exhibits a more typical
plug-in paradigm.

> I just though that there was something I could replace (*??*) with to get it
> to work. Apperently, Class<ActionManager> doesn't work, even though the
> class ActionManager has a static method setConnectionPool that takes a
> ConnectionPool object).

The static members of a class are not instance members of the
Corresponding Class object (which is how you are trying to use them).
Make the methods instance methods in the first place and invoke them on
an instance instead.

Signature

John Bollinger
jobollin@indiana.edu

Roedy Green - 28 Sep 2005 22:30 GMT
>    Have you looked at the Command pattern? It sounds like you're
>essentially trying to pass around methods as if they were objects, which is
>what the Command pattern allows you to emulate in Java.

The Java equivalent to method C's method parameters is the delegate.
See http://mindprod.com/jgloss/delegate. html

A delegate reference can be an interface with just one method in it, a
complex interface,  an abstract class, an actual class ...
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.