Java Reference
In-Depth Information
The output from this program is shown here:
Notice how TwoGen is declared:
It specifies two type parameters, T and V , separated by a comma. Because it has two type
parameters, two type arguments must be passed to TwoGen when an object is created, as
shown next:
In this case, Integer is substituted for T , and String is substituted for V . Although the two
type arguments differ in this example, it is possible for both types to be the same. For ex-
ample, the following line of code is valid:
In this case, both T and V would be of type String . Of course, if the type arguments were
always the same, then two type parameters would be unnecessary.
The General Form of a Generic Class
The generics syntax shown in the preceding examples can be generalized. Here is the syn-
tax for declaring a generic class:
class class-name < type-param-list > { // …
Here is the full syntax for declaring a reference to a generic class and creating a generic
instance:
class-name < type-arg-list > var-name =
new class-name < type-arg-list >( cons-arg-list );
Search WWH ::




Custom Search