Java Reference
In-Depth Information
In this case, both T and V will be replaced by String . This makes both versions of set( )
identical, which is, of course, an error.
Second, and more fundamental, is that the type erasure of set( ) effectively reduces both
versions to the following:
Thus, the overloading of set( ) as attempted in MyGenClass is inherently ambiguous. The
solution in this case is to use two separate method names rather than trying to overload set(
) .
Some Generic Restrictions
There are a few restrictions that you need to keep in mind when using generics. They in-
volve creating objects of a type parameter, static members, exceptions, and arrays. Each is
examined here.
Type Parameters Can't Be Instantiated
It is not possible to create an instance of a type parameter. For example, consider this class:
Here, it is illegal to attempt to create an instance of T . The reason should be easy to un-
derstand: the compiler has no way to know what type of object to create. T is simply a
placeholder.
Restrictions on Static Members
No static member can use a type parameter declared by the enclosing class. For example,
both of the static members of this class are illegal:
Search WWH ::




Custom Search