How can I check if a method is static using reflection?

后端 未结 3 2067
忘了有多久
忘了有多久 2021-01-30 12:32

I want to discover at run-time ONLY the static Methods of a class, how can I do this? Or, how to differentiate between static and non-static methods.

3条回答
  •  自闭症患者
    2021-01-30 12:39

    You can get the static methods like this:

    for (Method m : MyClass.class.getMethods()) {
       if (Modifier.isStatic(m.getModifiers()))
          System.out.println("Static Method: " + m.getName());
    }
    

提交回复
热议问题