Java Reference
In-Depth Information
18.2. Type Imports
Type imports are a generalization of the static imports that you learned
about in Chapter 2 on page 71 . They allow you to refer to types by their
simple names instead of having to write their fully qualified name.
When you write code outside a package that needs types declared within
that package you have two options. One is to use the fully qualified name
of the type. This option is reasonable if you use only a few items from
a package, but given the long names that packages tend to acquire (or
even the shorter ones in the standard libraries) it can be quite tedious to
use fully qualified names.
The other way to use types from a package is to import part or all of the
package. A programmer who wants to use the attr package could put the
following line near the top of a source file (after any package declaration
but before anything else):
import attr.*;
Now the types in that package can be referred to simply by name, such
as Attributed . An import that uses a * is called an import on demand de-
claration. You can also perform a single type import:
import attr.Attributed;
The name used with an import statement must be the canonical name of
a package (for import on demand) or type (for single type import). As
discussed in " Naming Classes " on page 411 , the canonical name and fully
qualified name can differ only for nested types. Note that generic types
are imported using their raw type namethat name can then be used in a
parameterized type without further qualification.
 
Search WWH ::




Custom Search