is it possible for me to call a static method if all I know is the class?

Recommended Answers

All 7 Replies

Well, yes, static methods can be called directly on the class by ClassName.methodName(). Is that all you needed to know or is there something more specific to your question?

I'm sorry, I should have been more specific. Here's an example of my problem

public class Test
{
    public static void main()
    {
        Class<? extends Object> c = Mystery.class;
        int x = getSomeNumber(c);
        System.out.println(x);
    }
    
    prviate static int getSomeNumber(Class<? extends Object> c)
    {
        if (c == Mystery.class)
        {
            Class<Mystery> thing = (Class<Mystery>) c;
            return thing.class.getNumber(); // compile time error
            // is there some way to call getNumber()?
        }
        return 0;
    }
}

class Mystery
{
    public static int getNumber()
    { return 1; }
}

In addition to what Ezzaral said, in any good IDE or editor you should be able to get a list of static methods as long as you just type in the class name.
Your

return thing.class.getNumber(); // compile time error

is not right. You have to call it by

return Mystery.getNumber()

I don't know why you are calling thing.class? or why are you creating "thing" or why are you converting c to Class object? :-| (information overload? :P). You don't have to do any of those.

If I am not wrong, I think you are trying to get the value for a specific object. In that case, static method is not your solution. You should not use static methods at any case but some particular situation. A static method sets something for a specific class (note: not for a specific object).

My problem is in a much larger program, where there would be a large amount of classes that extend the Mystery class. Say I have class John and John extends Mystery, and overrides the static method getNumber(); Thats why I can't just call Mystery.getNumber();

Or, is there any way to make an object of the Class<?> that was passed in the parameter and then call a method on that? Either way will work.

If you must know, the original code mimics the Zelda game, and I have a large array of Squares, and a whole bunch (around 20) of classes that extend it. When I load the program, it generates a map from a file, consisting of an array of integers, each corresponding to a certain Object. I want to be able to send a Class as a parameter instead of making a new Object and sending that as it would reduce the amount of time it takes for the game to load, and just generally better me as a programmer. But I can and am currently doing without.

Well, you can do it with reflection like this

private static int getSomeNumber(Class<? extends Object> c)
{
    if (c == Mystery.class)
    {
        try {
            Class<Mystery> thing = (Class<Mystery>)c;
            Method m = thing.getDeclaredMethod("getNumber", (Class[])null);
            return (Integer)m.invoke(null, (Object[])null); 
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    return 0;
}

But whether you should is another thing altogether. What is the fear of using objects there? Reflection isn't exactly the fastest thing in the world either, so if you're worried about creation time of objects then you may find using reflection no better. Are the objects very expensive to create? If so, can you use lazy initialization to defer some of that expense and keep the contructor light?

I'd say there are more design issues raised by this than answered.

Not so much that it's costly, I'd like to be able to declare a few public final variables that are calculated by the Object I'm sending the Class to. Just one question, can I use this reflection thingy to make constructors to? Reflection isn't taught in my textbook so I guess I'll go read up on it.

Anyway, this isn't really a major problem that stops my code and I can't really think of a situation where you would absolutely need it. I just figured there had to a way to do it and wanted to know what it was...

That answers my question, this thread is solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.