Class clazz = MyClass.class; Method method = clazz.getMethod("myPublicMethod", int.class); Object result = method.invoke(new MyClass(), 10);
Class clazz = MyClass.class; Method method = clazz.getMethod("myPublicStaticMethod", String.class); Object result = method.invoke(null, "hello");In this example, we get the method object for a public static method named "myPublicStaticMethod" that takes a String parameter from the `MyClass` class. We then invoke the method using a null instance (since the method is static) and passing in a value of "hello", and store the result in the `result` variable. Both of these examples use the `java.lang.reflect` package, which is a part of the core Java library for reflection purposes.