Java Reference
In-Depth Information
Type imports can also be used with nested class names. For example,
class. If these classes were in the package
bank
and you imported
bank.BankAccount
, you would still need to use
BankAccount.Permissions
to name the
Permissions
class. However, you could instead import
bank.BankAccount.Permissions
and then use the simple name
Permissions
.
You don't have to import
BankAccount
to import the class
BankAc-
count.Permissions
. Importing nested type names in this way should gen-
erally be avoided because it loses the important information that the
type is actually a nested type.
Static nested types can be imported using either the type import mech-
anism or the static import mechanism. If you insist on importing nested
types, then you should use the type import mechanism for consistency.
The
package
and
import
mechanisms give programmers control over po-
tentially conflicting names. If a package used for another purpose, such
as linguistics, has a class called
Attributed
for language attributes, pro-
grammers who want to use both types in the same source file have sev-
eral options:
•
Refer to all types by their fully qualified names, such as
at-
tr.Attributed
and
lingua.Attributed
.
•
Import only
attr.Attributed
or
attr.*
, use the simple name
At-
tributed
for
attr.Attributed
, and use the full name of
lin-
gua.Attributed
.
•
Do the converse: import
lingua.Attributed
or
lingua.*
, use the
simple name
Attributed
for
lingua.Attributed
, and use the full
name of
attr.Attributed
.
•
Import all of both packages
attr.*
and
lingua.*
and use the fully
qualified names
attr.Attributed
and
lingua.Attributed
in your
code. (If a type with the same name exists in two packages im-
ported on demand, you cannot use the simple name of either
type.)