aspose file tools*
The moose likes Beginning Java and the fly likes invoking static methods on Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "invoking static methods on Class" Watch "invoking static methods on Class" New topic
Author

invoking static methods on Class

Grub de Bliek
Greenhorn

Joined: Sep 09, 2003
Posts: 19
Hello,

I have a question I can't find the answer for. Maybe here I can find it.

In short the question is: can I invoke static methods of a certain class when I only have the Class object?

The situation is this:
1. I have a class com.company.tool.SomeThing with method public static void methodA() and field public static final String SOME_FIELD.
2. I do not want to instantiate this class.
3. I just have the class name: (com.company.tool.SomeThing)

What I'm trying is this:


I also want to retrieve the static public field but of course that won't work either. I really don't want to instantiate the class, but maybe I should consider to make it a Singleton with non static methods.


When the compiler isn't happy, nobody is happy...
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Why can't you just do this:

com.company.tool.SomeThing.methodA()

The way you refer to static methods with in classes is with <classname>.<static_method_name>


-Rich, SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Grub de Bliek:
Hello,

I have a question I can't find the answer for. Maybe here I can find it.

In short the question is: can I invoke static methods of a certain class when I only have the Class object?

The situation is this:
1. I have a class com.company.tool.SomeThing with method public static void methodA() and field public static final String SOME_FIELD.
2. I do not want to instantiate this class.
3. I just have the class name: (com.company.tool.SomeThing)

What I'm trying is this:


I also want to retrieve the static public field but of course that won't work either. I really don't want to instantiate the class, but maybe I should consider to make it a Singleton with non static methods.


The whole point of static methods is that you DON'T have to instantiate the class. As Richard pointed out, you can just use <ClassName>.<methodName>() to call the method directly. If you are doing this from a class in a different package, then you may want to use an import statement instead of the fully qualified class name.

Layne


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you just have the classname in a string, you're probably looking for reflection. Create a class object with Class.forName(string) and then look at Class in the JavaDoc to see how to find and invoke methods.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Grub de Bliek
Greenhorn

Joined: Sep 09, 2003
Posts: 19
If I had the class I could of course invoke a static method on it, like this:
com.company.tool.SomeThing.methodA();

But all I have is a String, ie "com.company.tool.SomeThing". Of course I must have the class first before I can invoke the static method. So how do I convert "com.company.tool.SomeThing" into com.company.tool.SomeThing

Because I want to invoke static methods, there's no point in instantiating the class. Also I prefer not to use reflection.
So my question is: is there a way to invoke static methods on a class I only have the class name of, preferably without using reflection?
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
So my question is: is there a way to invoke static methods on a class I only have the class name of, preferably without using reflection?


This question doesn't make much sense. If you have the class name then just do


Are you saying you don't know the name of the class ahead of time and will be receiving it via a string? If so get the name of the class from the string (maybe using substring or something similar) and proceed as above.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

You can't do this without reflection.

You can:



But you cannot:


Because the aClass is a reference of type Class, and the Class class does not respond to the call methodA()


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invoking static methods on Class