Close

Java Reflection - Class.getMethod() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

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

Returns public Method by it's name and parameters' types for this class object. The method might be declared in a super class or interface but it has to be public method.

Parameters:
name - the name of the method
parameterTypes - the list of parameters
Returns:
the Method object that matches the specified name and parameterTypes

Examples


package com.logicbig.example.clazz;

import java.lang.reflect.Method;

public class GetMethodExample {

public static void main(String... args) throws NoSuchMethodException {
Class<GetMethodExample> c = GetMethodExample.class;

Method m = c.getMethod("calcInt", int.class);
System.out.println(m);

m = c.getMethod("doSomething");
System.out.println(m);

m = c.getMethod("aStaticMethod", String.class);
System.out.println(m);

m = c.getMethod("doSomething");
System.out.println(m);

//inherited public methods
m = c.getMethod("toString");
System.out.println(m);

m = c.getMethod("equals", Object.class);
System.out.println(m);
}

public int calcInt(int i) {return 0;}

public void doSomething() {}

public static void aStaticMethod(String s) {}
}

Output

public int com.logicbig.example.clazz.GetMethodExample.calcInt(int)
public void com.logicbig.example.clazz.GetMethodExample.doSomething()
public static void com.logicbig.example.clazz.GetMethodExample.aStaticMethod(java.lang.String)
public void com.logicbig.example.clazz.GetMethodExample.doSomething()
public java.lang.String java.lang.Object.toString()
public boolean java.lang.Object.equals(java.lang.Object)




Attempting to get non public method will end up in NoSuchMethodException:

package com.logicbig.example.clazz;

import java.lang.reflect.Method;

public class GetMethodExample2 {

public static void main(String... args) throws NoSuchMethodException {
Class<GetMethodExample2> c = GetMethodExample2.class;
Method m = c.getMethod("doSomething");
System.out.println(m);
}

private void aMethod() {}
}

Output

Caused by: java.lang.NoSuchMethodException: com.logicbig.example.clazz.GetMethodExample2.doSomething()
at java.base/java.lang.Class.getMethod(Class.java:2065)
at com.logicbig.example.clazz.GetMethodExample2.main(GetMethodExample2.java:15)
... 6 more




Getting an interface method:

package com.logicbig.example.clazz;

import java.lang.reflect.Method;

public class GetMethodExample3 {

public static void main(String... args) throws NoSuchMethodException {
Class<Runnable> c = Runnable.class;
Method m = c.getMethod("run");
System.out.println(m);
}
}

Output

public abstract void java.lang.Runnable.run()




See Also