Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
Next,Tis used to declare an object calledob, as shown here:

T ob; // declare an object of type T

As explained,Tis a placeholder for the actual type that will be specified when aGenobject
is created. Thus,obwill be an object of the type passed toT. For example, if typeStringis
passed toT, then in that instance,obwill be of typeString.
Now considerGen’s constructor:

Gen(T o) {
ob = o;
}

Notice that its parameter,o, is of typeT. This means that the actual type ofois determined
by the type passed toTwhen aGenobject is created. Also, because both the parametero
and the member variableobare of typeT, they will both be of the same actual type when a
Genobject is created.
The type parameterTcan also be used to specify the return type of a method, as is the
case with thegetob( )method, shown here:

T getob() {
return ob;
}

Becauseobis also of typeT, its type is compatible with the return type specified bygetob( ).
TheshowType( )method displays the type ofTby callinggetName( )on theClassobject
returned by the call togetClass( )onob. ThegetClass( )method is defined byObjectand is
thus a member of all class types. It returns aClassobject that corresponds to the type of the
class of the object on which it is called.Classdefines thegetName( )method, which returns
a string representation of the class name.
TheGenDemoclass demonstrates the genericGenclass. It first creates a version ofGen
for integers, as shown here:

Gen<Integer> iOb;

Look closely at this declaration. First, notice that the typeIntegeris specified within the
angle brackets afterGen. In this case,Integeris atype argumentthat is passed toGen’s type
parameter,T. This effectively creates a version ofGenin which all references toTare translated
into references toInteger. Thus, for this declaration,obis of typeInteger, and the return type
ofgetob( )is of typeInteger.
Before moving on, it’s necessary to state that the Java compiler does not actually create
different versions ofGen, or of any other generic class. Although it’s helpful to think in
these terms, it is not what actually happens. Instead, the compiler removes all generic type
information, substituting the necessary casts, to make your codebehave as ifa specific version
ofGenwere created. Thus, there is really only one version ofGenthat actually exists in your
program. The process of removing generic type information is callederasure,and we will
return to this topic later in this chapter.

318 Part I: The Java Language

Free download pdf