10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TYPES, VALUES, AND VARIABLES Raw Types 4.8<br />

<strong>The</strong> use of raw types is allowed only as a concession to compatibility of legacy<br />

code. <strong>The</strong> use of raw types in code written after the introduction of genericity<br />

into the <strong>Java</strong> programming language is strongly discouraged. It is possible that<br />

future versions of the <strong>Java</strong> programming language will disallow the use of raw<br />

types.<br />

It is a compile-time error to attempt to use a type member of a parameterized<br />

type as a raw type.<br />

DISCUSSION<br />

This means that the ban on "rare" types extends to the case where the qualifying type is<br />

parameterized, but we attempt to use the inner class as a raw type:<br />

Outer.Inner x = null; // illegal<br />

This is the opposite of the case we discussed above. <strong>The</strong>re is no practical justification<br />

for this half baked type. In legacy code, no type parameters are used. In non-legacy code,<br />

we should use the generic types correctly and pass all the required actual type parameters.<br />

DISCUSSION<br />

Variables of a raw type can be assigned from values of any of the type’s parametric<br />

instances.<br />

For instance, it is possible to assign a Vector to a Vector, based on the<br />

subtyping rules (§4.10.2).<br />

<strong>The</strong> reverse assignment from Vector to Vector is unsafe (since the raw vector<br />

might have had a different element type), but is still permitted using unchecked conversion<br />

(§5.1.9) in order to enable interfacing with legacy code. In this case, a compiler will issue an<br />

unchecked warning.<br />

DRAFT<br />

<strong>The</strong> superclasses (respectively, superinterfaces) of a raw type are the erasures of<br />

the superclasses (superinterfaces) of any of its parameterized invocations.<br />

<strong>The</strong> type of a constructor (§8.8), instance method (§8.8, §9.4), or non-static<br />

field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces<br />

is the erasure of its type in the generic declaration corresponding to C.<br />

<strong>The</strong> type of a static member of a raw type C is the same as its type in the generic<br />

declaration corresponding to C.<br />

It is a compile-time error to pass actual type parameters to a non-static type<br />

member of a raw type that is not inherited from its superclasses or superinterfaces.<br />

59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!