Java Reference
In-Depth Information
Before sort: (100.0, 200.0, 300.0) (10.0, 20.0, 30.0)
After sort: (10.0, 20.0, 30.0) (100.0, 200.0, 300.0)
Note
Type parameters cannot have lower bounds. Angelika Langer explains the
rationale for this restriction in her “Java Generics FAQs” (see
ht-
Type Parameter Scope
Atypeparameter'sscope(visibility)isitsgenerictypeexceptwheremasked(hidden).
This scope includes the formal type parameter list of which the type parameter is
a member. For example, the scope of
S
in
SortedShapesList<S extends
Shape&Comparable<S>>
isallof
SortedShapesList
andtheformaltypepara-
meter list.
Itispossibletomaskatypeparameterbydeclaringasame-namedtypeparameterin
anestedtype'sformaltypeparameterlist.Forexample,
Listing3-54
masksanenclosing
class's
T
type parameter.
Listing 3-54.
Masking a type variable
class EnclosingClass<T>
{
static class EnclosedClass<T extends Comparable<T>>
{
}
}
EnclosingClass
's
T
type parameter is masked by
EnclosedClass
's
T
type
parameter, which specifies an upper bound where only those types that implement the
Comparable
interfacecanbepassedto
EnclosedClass
.Referencing
T
fromwith-
in
EnclosedClass
referstothebounded
T
andnottheunbounded
T
passedto
En-
closingClass
.
Ifmaskingisundesirable,itisbesttochooseadifferentnameforthetypeparameter.
For example, you might specify
EnclosedClass<U extends Compar-
able<U>>
. Although
U
is not as meaningful a name as
T
, this situation justifies this
choice.