Method: getModifiers() : Method « java.lang.reflect « Java by API






Method: getModifiers()

  
/*
 * Output:
Public Methods:
 a1
 a2
 * */

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MainClass {
  public static void main(String args[]) {
    try {
      MyClass a = new MyClass();
      Class c = a.getClass();
      System.out.println("Public Methods:");
      Method methods[] = c.getDeclaredMethods();
      for (int i = 0; i < methods.length; i++) {
        int modifiers = methods[i].getModifiers();
        if (Modifier.isPublic(modifiers)) {
          System.out.println(" " + methods[i].getName());
        }
      }
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}

class MyClass {
  public void a1() {
  }

  public void a2() {
  }

  protected void a3() {
  }

  private void a4() {
  }
}

           
         
    
  








Related examples in the same category

1.Method: getAnnotation(Class < MyAnno > annotationClass)
2.Method: getExceptionTypes()
3.Method: getName()
4.Method: getParameterTypes()
5.Method: getReturnType()
6.Method: invoke(Object obj, Object... args)
7.Method: setAccessible(boolean flag)
8.Method: toGenericString()
9.Method: toString()