Java SE API
Class.getMethod()
Look up a public method, including inherited public methods. See official API for exceptions and overloads; Java 21-compatible example.
Formal Syntax & Structure
java syntaxjava
Method getMethod(String name, Class<?>... parameterTypes)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | String | Public method name |
| parameterTypes | Class<?>... | Exact formal parameter types |
Return Value
Method
Look up a public method, including inherited public methods.
Working Example
Example Snippetjava
import java.lang.annotation.*;import java.lang.reflect.*;import java.lang.invoke.*;import java.util.*;import java.nio.file.*;import java.nio.charset.StandardCharsets;import java.net.URLClassLoader;import java.net.URL;public class Main { public static class Parent { public String name() { return "report"; } } public static class Report extends Parent { public int pages = 3; private int secret = 7; public String format(int count) { return "pages=" + count; } public void reject() { throw new IllegalStateException("invalid"); } public List<String> tags = List.of("daily"); } public static void main(String[] args) throws Throwable { Method m = Report.class.getMethod("name"); System.out.println(m.getDeclaringClass().getSimpleName()); System.out.println(m.invoke(new Report())); System.out.println(Report.class.getDeclaredMethod("format", int.class).getReturnType().getSimpleName()); }}