Quantcast
Channel: silveira neto » Java
Viewing all articles
Browse latest Browse all 11

Java: invoking a method by name

$
0
0


import java.lang.reflect.*;
 
public class Foo {
	public void bar(int param){
		System.out.println(param);
	}	
 
	public static void main(String args[]){
		Object f = new Foo();
		try {
			Method m = f.getClass().getMethod("bar", int.class);
			m.invoke(f, 42);
		} catch (Exception e){
			System.err.println(e);	
		}
	}
 
}

$ java Foo
42


Viewing all articles
Browse latest Browse all 11

Trending Articles