Is it possible for a generic method in Java to call a specific other method based on the type of the input?
I tried the following example which hopefully represents the problem.
class X {
public void pr(Object s) {
System.out.println("Object");
}
public void pr(String s) {
System.out.println("String");
}
public <T> void go(T x) {
this.pr(x);
}
public static void main(String[] args) {
String v = "hello";
X x = new X();
// Both print "Object", while I want "String"
x.go(v);
x.<String>go(v);
}
}
I would want go() to call pr(String). But whatever I do, pr(Object) is called.
The example above is for demonstration only, real application of this is much more complex.
Thanks in advance.