Show public methods in Java

Description

The following code shows how to show public methods.

Example


/*from w ww .  j  av  a 2 s  . c o  m*/
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
  public static void main(String args[]) {
    try {
      A a = new A();
      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 A {
  public void a1() {
  }

  public void a2() {
  }

  protected void a3() {
  }

  private void a4() {
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy