Get method from a class by name in Java

Description

The following code shows how to get method from a class by name.

Example


//from  w  w w.  ja v a2 s .  c  o m
import java.lang.reflect.Method;

public class Main {
  public static void main(String[] argv) {
    try {
      Class c = Main.class;
      Method m = c.getMethod("main"); // warning

      // production code should handle this exception more gracefully
    } catch (NoSuchMethodException x) {
      x.printStackTrace();
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy