Java Reference
In-Depth Information
All classes belong to some package. If a class definition does not include the
package directive, then it goes into the default unnamed package maintained
internally by the JVM. Note that by convention package names begin with a lower-
case letter while class names begin with an upper-case letter. In fact, package
names are often completely lower case, as is true of all but 28 of the 135 packages
in J2SE 1.4.2. (Those 28 packages with upper-case characters are all part of the
CORBA system in Java, which we discuss in Chapter 19, and which use upper-
case names for consistency with the CORBA naming conventions.)
5.3.1 Namespaces
Packages were also designed to provide namespace protection - that is, in order to
avoid name collisions. In our examples above, we used the not uncommon names
TestA , TestB , etc. If we kept all of our source code in one large directory, we
would have to think of more creative names to avoid confusing the various test
classes we might write. By organizing our classes in packages, which means they
must be organized on disk in the same directory hierarchy as used for the package
names, we have a way to keep like classes together and unlike classes somewhere
else. That way, we could create a package named basic for the simple test classes
from the first five chapters of this topic and a package named graphics for our
graphics demos, etc.
Now consider the commercial Java packages one might purchase. Several
different competitive companies might offer Java graphics software, for example,
each with similar types of graphing classes, and each with similar or perhaps even
identical names for some of the classes. If you have two or more such software
libraries installed, how do you distinguish between them? The answer is with
packages. Each vendor should create a separate package name for that vendor's
offerings. Still, there could be naming conflicts if two vendors choose identical
package names.
Suppose vendors ABC and XYZ both create a PieChart graphing class. To
keep the two different PieChart classes separate, we would hope that one might
be in a package named, for example, abc ,while the other PieChart class would
be in a package named xyz . Thus you would refer to either abc.PieChart or
xyz.PieChart , depending on which version you wanted.
An ingenious naming convention promulgated by Sun from the earliest days
of Java is to name packages based on a company's internet domain name with
the order of the names reversed. Since domain names are unique, package names
based on domain names are completely under the control of the company that
owns that domain name. Thus our companies ABC and XYZ would probably
name their packages com.abc and com.xyz , respectively.
In practice, company ABC might have several products - say a scientific
graphics package and a business graphics package, along with other non-
graphics classes. Thus, it might offer several packages - com.abc.graphics.
scientific , com.abc.graphics.business , and com.abc.utils , etc.
Search WWH ::




Custom Search