Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how to use reflection to get a static method and execute it? [duplicate]

Tags:

java

I have a bunch of static method names, how do I execute them. I think I can use reflection, but how to do that?

like image 758
user496949 Avatar asked Jan 12 '12 14:01

user496949


1 Answers

directly from the interwebz

Class<?> class1;
    try {
        class1 = Class.forName(CLASS);
        Method method = class1.getMethod(METHOD, String.class);
        Object o = method.invoke(null, NAME);
        System.out.println(o);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
like image 176
hvgotcodes Avatar answered Sep 23 '22 11:09

hvgotcodes