Java Reference
In-Depth Information
These type variables are known as the type parameters of the class. The type parameter
section follows the class name and is delimited by angle brackets.
TypeParameters:
< TypeParameterList >
TypeParameterList:
TypeParameterList , TypeParameter
TypeParameter
In a class's type parameter section, a type variable T directly depends on a type variable S
if S is the bound of T , while T depends on S if either T directly depends on S or T directly
depends on a type variable U that depends on S (using this definition recursively).
It is a compile-time error if a type variable in a class's type parameter section depends on
itself.
The scope and shadowing of a class's type parameter is specified in § 6.3 and § 6.4 .
Example 8.1.2-1. Mutually Recursive Type Variable Bounds
Click here to view code image
interface ConvertibleTo<T> {
T convert();
}
class ReprChange<T extends ConvertibleTo<S>,
S extends ConvertibleTo<T>> {
T t;
void set(S s) { t = s.convert(); }
S get()
{ return t.convert(); }
}
A generic class declaration defines a set of parameterized types (§ 4.5 ) , one for each pos-
sible invocation of the type parameter section by type arguments. All of these parameter-
ized types share the same class at run time.
For instance, executing the code:
Click here to view code image
Vector<String> x = new Vector<String>();
Vector<Integer> y = new Vector<Integer>();
boolean b = x.getClass() == y.getClass();
will result in the variable b holding the value true .
Search WWH ::




Custom Search