Java usually prefers the usual methods to the general ones when choosing an overloaded method that can generate the following sscce :
public class GenericsTest { public static void main(String[] args) { myMethod(Integer.class, 10); myMethod(String.class, "overloaded method"); } public static <T> void myMethod(Class<T> klass, T foo) { System.out.println("hello world"); } public static <T> void myMethod(Class<T> klass, String bar) { System.out.println(bar); } }
Output:
hello world overloaded method
Is there a way to get Java to use the universal version?
java generics overloading
durron597
source share