Java Reflection - Java Class.getMethod(String name, Class <?>... parameterTypes)








Syntax

Class.getMethod(String name, Class <?>... parameterTypes) has the following syntax.

public Method getMethod(String name, Class<?>... parameterTypes)
               throws NoSuchMethodException,
                      SecurityException 

Example

In the following code shows how to use Class.getMethod(String name, Class <?>... parameterTypes) method.

//from  ww  w . ja  v  a 2  s. c  om

import java.lang.reflect.Method;

public class Main {
  public static void main(String[] args) throws Exception {
    Main cls = new Main();
    Class c = cls.getClass();

    // parameter type is null
    Method m = c.getMethod("show", null);
    System.out.println("method = " + m.toString());

    // method Long
    Class[] cArg = new Class[1];
    cArg[0] = Long.class;
    Method lMethod = c.getMethod("showLong", cArg);
    System.out.println("method = " + lMethod.toString());
  }
}

class MyClass {

  public Integer show() {
    return 1;
  }

  public void showLong(Long l) {
    this.l = l;
  }

  long l = 1L;
}

The code above generates the following result.