[ACCEPTED]-How can I check if a method is static using reflection?-reflection

Accepted answer
Score: 190

Use Modifier.isStatic(method.getModifiers()).

 * Returns the public static methods of a class or interface,
 *   including those declared in super classes and interfaces.
public static List<Method> getStaticMethods(Class<?> clazz) {
    List<Method> methods = new ArrayList<Method>();
    for (Method method : clazz.getMethods()) {
        if (Modifier.isStatic(method.getModifiers())) {
    return Collections.unmodifiableList(methods);

Note: This method is actually dangerous 11 from a security standpoint. Class.getMethods 10 "bypass[es] SecurityManager checks depending 9 on the immediate caller's class loader" (see 8 section 6 of the Java secure coding guidelines).

Disclaimer: Not 7 tested or even compiled.

Note Modifier should be 6 used with care. Flags represented as ints 5 are not type safe. A common mistake is to 4 test a modifier flag on a type of reflection 3 object that it does not apply to. It may 2 be the case that a flag in the same position 1 is set to denote some other information.

Score: 15

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


Score: 5

To flesh out the previous (correct) answer, here 2 is a full code snippet which does what you 1 want (exceptions ignored):

public Method[] getStatics(Class<?> c) {
    Method[] all = c.getDeclaredMethods()
    List<Method> back = new ArrayList<Method>();

    for (Method m : all) {
        if (Modifier.isStatic(m.getModifiers())) {

    return back.toArray(new Method[back.size()]);

More Related questions