Suppose below class
(without any errors or even warnings):
class C<T> {
C(C1<T> c1) { /*...*/ }
class C1<U> { /*...*/ }
}
Now, how can I make a new instance of it?
new C<String>(new C1<String>()) {}; // Error behind `<String>`: Type arguments given on a raw type
// + Warning behind `new C1<String>()`: Unchecked assignment: 'com.example.package.C.C1<java.lang.String>' to 'com.example.package.C<java.lang.String>.C1<java.lang.String>'type
new C<String>(new C1<>()) {}; // Error behind `<>`: Type arguments given on a raw type
new C<String>(new C1()) {}; // Warning behind `new C1()`: Unchecked assignment: 'com.example.package.C.C1' to 'com.example.package.C<java.lang.String>.C1<java.lang.String>'
Although the third way doesn't include an error, that isn't what I want! I want new C1<String>
. Also, it includes a warning.
Note that the problem is only when C1
is an inner class
(not static
) of C
. For example, there is no problem with below code:
class C<T> {
C(C2<T> c1) { /*...*/ }
//class C1<U> { /*...*/ }
}
class C2<V> { /*...*/ }
...
new C<String>(new C2<String>()) {}; // OK
new C<String>(new C2<>()) {}; // OK (and is equivalent)