Jump to content

Send an unknown method call: Difference between revisions

added java
(added java)
Line 46:
 
For more information on this see [[Respond_to_an_unknown_method_call#Icon_and_Unicon|Respond to an unknown method call]].
 
=={{header|Java}}==
Using reflection
<lang java>import java.lang.reflect.Method;
 
class Example {
public int foo(int x) {
return 42 + x;
}
}
 
public class Main {
public static void main(String[] args) throws Exception {
Object example = new Example();
String name = "foo";
Class<?> clazz = example.getClass();
Method meth = clazz.getMethod(name, int.class);
Object result = meth.invoke(example, 5); // result is int wrapped in an object (Integer)
System.out.println(result); // prints "47"
}
}</lang>
 
=={{header|JavaScript}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.