I have defined the following general class, but when I use it in a class object, it does not compile. The constructor will not accept another 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 didn't the first compile and the second compile?
EDIT: It compiled in Java 8
source share