Let's consider a class:
public class Foo<T> {
public List<String> list = new ArrayList<>();
}
that I'm passing as a parameter to a method.
I have a problem understanding why here the type String
isn't resolved:
public void test(Foo t) {
t.list.get(0).contains("test");
}
and the t.list
is treated as List<Object>
while here everything works just fine:
public void test(Foo<?> t) {
t.list.get(0).contains("test");
}
and t.list
is List<String>
.
The other questions about Type Erasure that have been linked, approach the problem from a different angle. Without knowing the answer to my own question I failed to see the connection and it is why I do not think this question is a duplicate.
Which of these Exception handlers cannot be type parameterized? Explanation: we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.
Cannot Declare Static Fields Whose Types are Type Parameters. Cannot Use Casts or instanceof With Parameterized Types. Cannot Create Arrays of Parameterized Types. Cannot Create, Catch, or Throw Objects of Parameterized Types.
Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java? Explanation: None.
3. Which of these data type cannot be type parameterized? Explanation: None.
When using Foo t
, t
is a Raw Type so its non-static, non-inherited members, like the list
in above code, are also raw types. Here the relevant part of the Java Language Specification (see above link):
To facilitate interfacing with non-generic legacy code, it is possible to use as a type the erasure (§4.6) of a parameterized type (§4.5) or the erasure of an array type (§10.1) whose element type is a parameterized type. Such a type is called a raw type.
More precisely, a raw type is defined to be one of:
. . .
A non-static member type of a raw type R that is not inherited from a superclass or superinterface of R.
and/or
The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.
Just declare the list
as static
and it will be interpreted as a List
of String
as expected (for testing).
On the other side, the declaration Foo<?>
is not a raw type and consequently the list
is also not considered a raw type.
An advice later on that page:
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
Note: Type Erasure and generated byte-code is the same in both cases...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With