Ben Ben - 3 years ago 194
Java Question

What does `new Class<?>[] {}` mean?


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);

What does
new Class<?>[] {}
mean? Thanks.

Answer Source

That creates an empty array of type Class<?> (which is a wild card capture of Classes). 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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download