Consider this very simple generic class:
class GenericTest<T> {
T t;
GenericTest(T t) {
this.t = t;
}
}
I created multiple objects of this class, one of them is raw type:
class App {
public static void main(String[] args) {
GenericTest<String> test1 = new GenericTest<>("Test 1");
GenericTest<String> test2 = new GenericTest<>("Test 2");
GenericTest<String> test3 = new GenericTest<>("Test 3");
GenericTest raw = new GenericTest(1.0); // Line 19
test1 = raw; // Line 21
test2 = raw; // Line 22
raw = test3; // Line 23
}
}
when compiling the project it shows 3 warnings for these lines:
App.java:19: warning: [unchecked] unchecked call to GenericTest(T) as a member of the raw type GenericTest App.java:21: warning: [unchecked] unchecked conversion App.java:22: warning: [unchecked] unchecked conversion
My question is why it doesn't give a warning for the third assignment raw = test3;
?
One possible answer: because it does not cause any further loss of type safety that already had occurred.
- but it also was true when the second assignment
test2 = raw;
(line 22) so it should not issued any warning for the same reason, because it does not cause any further loss of type safety than line 21.