Java Class getMethod() Method
The getGenericSuperClass() method of java Class class returns a method object representing the specified public member method of the class or interface represented by this Class object. The name parameter is passed as a string.
Syntax
public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException
Parameter
name
- the name of the method
parameterTypes
- list of parameters
Returns
The Method object.
Throws
NoSuchMethodException , NullPointerException , SecurityException
Example 1
import java.lang.reflect.*;
public class ClassgetMethodExample1 {
public static void main(String[] args) {
ClassgetMethodExample1 class1 = new ClassgetMethodExample1();
Class cls = class1.getClass();
try {
Method mthd = cls.getMethod("showMethod", null);
System.out.println("method = " + mthd.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString()); //print exception object
try {
Class[] aarg = new Class[1];
aarg[0] = Long.class;
Method lmthd = cls.getMethod("showLongMethod", aarg);
System.out.println("method = " + lmthd.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString()); //print exception object
public Integer showMethod() {
return 1;
public void showLongMethod(Long lng) {
this.lng = lng;
long lng = 788995;
Test it Now
Output:
method = public java.lang.Integer ClassgetMethodExample1.showMethod()
method = public void ClassgetMethodExample1.showLongMethod(java.lang.Long)
Example 2
//import statements?
import java.lang.reflect.Method;
public class ClassgetMethodExample2 {
public static void main(String... args) throws NoSuchMethodException {
Class<ClassgetMethodExample2> class1 = ClassgetMethodExample2.class;
Method mthd = class1.getMethod("IntCalc", int.class);
System.out.println(mthd);
mthd = class1.getMethod("Work");
System.out.println(mthd);
mthd = class1.getMethod("StaticMethod", String.class);
System.out.println(mthd);
mthd = class1.getMethod("Work");
System.out.println(mthd);
public int IntCalc(int i) {return 0;}
public void Work() {}
public static void StaticMethod(String s) {}
Test it Now
Output:
public int ClassgetMethodExample2.IntCalc(int)
public void ClassgetMethodExample2.Work()
public static void ClassgetMethodExample2.StaticMethod(java.lang.String)
public void ClassgetMethodExample2.Work()
Next TopicJava Class