Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoking static methods on Class

 
Grub de Bliek
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Anderson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Grub de Bliek
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic