Reflection can locate a method by name and parameter types, then invoke it.

method metadata A Method object describes one method, including its name and parameter list.
invoke Calling invoke runs the reflected method with the supplied receiver and arguments.

Method Lookup

value
MethodLookup.java
Replay: real traced execution (multi-file project)
import java.lang.reflect.Method;

public class MethodLookup {
    static class Calculator {
        static int twice(int value) {
            return value * 2;
        }
    }

    public static void main(String[] args) throws Exception {
        int value = 4;
        Method method = Calculator.class.getDeclaredMethod("twice", int.class);
        int result = (int) method.invoke(null, value);

        System.out.println("result=" + result);
    }
}
import java.lang.reflect.Method;

public class MethodLookup {
    static class Calculator {
        static int twice(int value) {
            return value * 2;
        }
    }

    public static void main(String[] args) throws Exception {
        int value = 7;
        Method method = Calculator.class.getDeclaredMethod("twice", int.class);
        int result = (int) method.invoke(null, value);

        System.out.println("result=" + result);
    }
}
import java.lang.reflect.Method;

public class MethodLookup {
    static class Calculator {
        static int twice(int value) {
            return value * 2;
        }
    }

    public static void main(String[] args) throws Exception {
        int value = 10;
        Method method = Calculator.class.getDeclaredMethod("twice", int.class);
        int result = (int) method.invoke(null, value);

        System.out.println("result=" + result);
    }
}
  1. value ← 4, method ← static int MethodLookup$Calculator.twice(int)

    10public static void main(String[] args) throws Exception {11    int value→ 4 = 4;  //@value=7, 1012    Method method→ static int MethodLookup$Calculator.twice(int) = Calculator.class.getDeclaredMethod("twice", int.classint);13    int result = (int) method.invoke(null, value4);
  2. static int twice(int value)

    4static class Calculator {5    static int twice(int value4) {6        return value4 * 2;7    }
  3. result ← 8

    12    Method method = Calculator.class.getDeclaredMethod("twice", int.class);13    int result→ 8 = (int) method.invoke(null, value4);1415    System.out.println("result=" + result8);16}
    outputresult=8
  1. value ← 7, method ← static int MethodLookup$Calculator.twice(int)

    10public static void main(String[] args) throws Exception {11    int value→ 7 = 7;12    Method method→ static int MethodLookup$Calculator.twice(int) = Calculator.class.getDeclaredMethod("twice", int.classint);13    int result = (int) method.invoke(null, value7);
  2. static int twice(int value)

    4static class Calculator {5    static int twice(int value7) {6        return value7 * 2;7    }
  3. result ← 14

    12    Method method = Calculator.class.getDeclaredMethod("twice", int.class);13    int result→ 14 = (int) method.invoke(null, value7);1415    System.out.println("result=" + result14);16}
    outputresult=14
  1. value ← 10, method ← static int MethodLookup$Calculator.twice(int)

    10public static void main(String[] args) throws Exception {11    int value→ 10 = 10;12    Method method→ static int MethodLookup$Calculator.twice(int) = Calculator.class.getDeclaredMethod("twice", int.classint);13    int result = (int) method.invoke(null, value10);
  2. static int twice(int value)

    4static class Calculator {5    static int twice(int value10) {6        return value10 * 2;7    }
  3. result ← 20

    12    Method method = Calculator.class.getDeclaredMethod("twice", int.class);13    int result→ 20 = (int) method.invoke(null, value10);1415    System.out.println("result=" + result20);16}
    outputresult=20