public class Program {
private static <Program> void foo(Program x){
System.out.println(x+"-->1");
}
private static void foo(final int i){
System.out.println(i+"-->2");
}
public static void main(String[] args) {
Integer i = 10;
foo(i);
}
}
And the output is:
10-->1
I wasn't able to find any relevant discussion on this topic. However, the answer to a different topic confused me a little:- Return Type of Java Generic Methods
According to them the generic <Program>
has nothing to do with return type but in my case if I change a little to this program as below then the output is different.
public class Program {
private static <Integer> void foo(Program x){
System.out.println(x+"-->1");
}
private static void foo(final int i){
System.out.println(i+"-->2");
}
public static void main(String[] args) {
Integer i = 10;
foo(i);
}
}
Output:
10-->2
I am using JDK1.7
In your first example, You're not actually specifying argument of type Program
, it is a generic. That type parameter has nothing to do with your class named Program
. You will get same result by making a typo like this:
public class Program {
private static <Programmmm> void foo(Programmmm x){
System.out.println(x+"-->1");
}
private static void foo(final int i){
System.out.println(i+"-->2");
}
public static void main(String[] args) {
Integer i = 10;
foo(i);
}
}
However, in your second example, the parameter is literally of type Program
and so it doesn't match when called as foo(10);
and you get result from second method.