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?

6
  • 2
    It should be SomeClass.doSomething(); Commented Nov 12, 2018 at 9:36
  • cls = SomeClass wouldn't work with a non-static variable either. Did you mean cls = new SomeClass();? Commented Nov 12, 2018 at 9:36
  • 2
    What? You don't need a variable to call a static method... Commented Nov 12, 2018 at 9: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. Commented Nov 12, 2018 at 9:37
  • Then it should be private static Class<?> cls; and I would close this question as a duplicate of this one. Commented Nov 12, 2018 at 9:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
    }
}

1 Comment

You can. I fail to see why you would want to. Thanks for the information in any case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.