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 w ww  .  ja va 2 s.  co m*/

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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable