
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()