1

I need to store a reference to a class in a variable, such that I can call the static methods of that class on the variable.

Main.java

public class Main {

    private static SomeClass cls;

    private static void main(String[] args) {
        **cls = SomeClass;**
        cls.doSomething();
    }

SomeClass.java

public class SomeClass() {
    public static doSomething() {
    }
}

cls = SomeClass is not working here, but I also don't want to instantiate SomeClass.

Can anyone help?

Simon
  • 11
  • 2
  • 2
    It should be `SomeClass.doSomething();` – Guy Nov 12 '18 at 09:36
  • `cls = SomeClass` wouldn't work with a non-static variable either. Did you mean `cls = new SomeClass();`? – UnholySheep Nov 12 '18 at 09:36
  • 2
    What? You don't need a variable to call a static method... – shmosel Nov 12 '18 at 09:36
  • Thank you for the answers. See however the code. The idea is that cls can reference several classes based on some condition which is not provided in the code above. – Simon Nov 12 '18 at 09:37
  • Then it should be `private static Class> cls;` and I would close this question as a duplicate of [this one](https://stackoverflow.com/questions/2467544/invoking-a-static-method-using-reflection). – Robby Cornelissen Nov 12 '18 at 09:37
  • This is not what classes in Java are for. Please use an instance instead, it will make the code clearer. Have your instances belong to classes that share an interface that defines the methods you want to be able to call. Then use that interface as the type of your variable. – Ole V.V. Nov 12 '18 at 09:39

2 Answers2

4

This makes no sense.

You can write

private static SomeClass cls = null;

(or leave it unassigned, since the default value would be null anyway)

and

cls.doSomething()

will not throw NullPointerException and will call the static method.

However, there's no reason to do it. Regardless of what you assign to the cls variable, it will always call SomeClass.doSomething(), so it would make more sense to eliminate that variable and simply call SomeClass.doSomething().

The idea is that cls can reference several classes based on some condition which is not provided in the code above

This idea won't work. The compile time type of the cls variable will determine the class of the static method being called. Since it can only have a single type, it will always be the same static method.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

You can use reflection for this requirement. Below is example:

public class Reference {

    public static Class clazz = null;

    public static void main(String[] args) {
        try {
            refer(Something.class);
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void refer(Class clazzToBeCalled) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        clazz = clazzToBeCalled; //No need to store it on class level
        Method methodToBeCalled = clazz.getMethod("doSomething");
        methodToBeCalled.invoke(null);
    }
}
rahul kale
  • 11
  • 5