I have defined the following generic Class , But when I use it on the Class Object it doesn't compile. The constructor wouldn't accept other object
class Pair<T,V> {
T one;
V two;
public Pair(T one, V two) {
this.one = one;
this.two = two;
}
}
public static void main(String[] args) {
String hamza = "Hamza";
Integer soufiane = 0;
Pair<Object,Object> pairOne = new Pair<>(hamza, soufiane);
Pair<Object,Object> pairTwo = new Pair<Object, Object>(soufiane, hamza);
}
Error message:
incompatible types: Pair<String,Integer> cannot be converted to Pair<Object,Object>
Why did the first one not Compile and the second compile ?
EDIT: It compiled on Java 8