Java Reference
In-Depth Information
1.16. Packages
Name conflicts are a major problem when you're developing reusable
code. No matter how carefully you pick names for classes, someone else
is likely to use that name for a different purpose. If you use simple,
descriptive names, the problem gets worse since such names are more
likely to be used by someone else who was also trying to use simple, de-
scriptive names. Words like "list," "event," "component," and so on are
used often and are almost certain to clash with other people's uses.
The standard solution for name collision in many programming languages
is to use a package prefix at the front of every class, type, global func-
tion, and so on. Prefix conventions create naming contexts to ensure that
names in one context do not conflict with names in other contexts. These
prefixes are usually a few characters long and are usually an abbreviation
of the product name, such as Xt for the X-Windows Toolkit.
When code uses only a few packages, the likelihood of prefix conflict
is small. However, since prefixes are abbreviations, the probability of a
name conflict increases with the number of packages used.
The Java programming language has a formal notion of package that has
a set of types and subpackages as members. Packages are named and
can be imported. Package names are hierarchical, with components sep-
arated by dots. When you use part of a package, either you use its fully
qualified name the type name prefixed by the package name, separated
by a dotor you import all or part of the package. Importing all, or part,
of a package, simply instructs the compiler to look in the package for
types that it can't find defined locally. Package names also give you con-
trol over name conflicts. If two packages contain classes with the same
name, you can use the fully qualified class name for one or both of them.
Here is an example of a method that uses fully qualified names to print
the current day and time using the utility class Date (documented in
Chapter 22 ) , which, as with all time-based methods, considers time to be
in milliseconds since the epoch (00:00:00 GMT, January 1, 1970):
 
Search WWH ::




Custom Search