Calling static method on a class?

I’m not sure exactly what the situation is, but if you’re looking to execute the static method on a class without knowing the class type (i.e. you don’t know it’s SomeType, you just have the Class object), if you know the name and parameters of the method you could use reflection and do this:

Class c = getThisClassObjectFromSomewhere();

//myStaticMethod takes a Double and String as an argument
Method m = c.getMethod("myStaticMethod", Double.class, String.class);
Object result = m.invoke(null, 1.5, "foo");

Leave a Comment