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, descriptive 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 function, 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
separated by dots. When you use part of a package, either you use its fully qualified namethe 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 control 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):
class Date1 {
public static void main(String[] args) {
java.util.Date now = new java.util.Date();
System.out.println(now);
}
}
And here is a version that uses import to declare the type Date:
import java.util.Date;
class Date2 {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
}
}
When the compiler comes to the declaration of now it determines that the Date type is actually the
java.util.Date type, because that is the only Date type it knows about. Import statements simply
provide information to the compiler; they don't cause files to be "included" into the current file.
The name collision problem is not completely solved by the package mechanism. Two projects can still give
their packages the same name. This problem can be solved only by convention. The standard convention is to
use the reversed Internet domain name of the organization to prefix the package name. For example, if the
Acme Corporation had the Internet domain acme.com, it would use package names starting with
com.acme, as in com.acme.tools.