From https://stackoverflow.com/a/1079799
Java is by design not fit for duck typing. The way you might choose to
do it is reflection:
public void doSomething(Object obj) throws Exception {
obj.getClass().getMethod("getName", new Class<?>[] {}).invoke(obj);
}
new Class<?>[] {}
That creates an empty array of type Class<?>
(which is a wild card capture of Class
es). The array created is explicitly of length 0
. Which indicates that getName
takes no arguments (if it took arguments, the array would need to contain the appropriate classes to match the type signature).