Java Reference
In-Depth Information
A package need not have a single name. You can specify a package name as a sequence of names
separated by periods. For example, you might have developed several collections of classes dealing with
geometry, one dealing with 2D shapes and another with 3D shapes. In this case you might include the
class Sphere in a package with the statement:
package Geometry.Shapes3D;
and the class for circles in a package using the statement:
package Geometry.Shapes2D;
In this situation, the packages are expected to be in the directories Shapes3D and Shapes2D , and both
of these must be sub-directories of Geometry . In general, you can have as many names as you like
separated by periods to identify a package, but the name must reflect the directory structure where the
package is stored.
Package Geometry.Shapes2D
Package Geometry.Shapes3D
Compiling a Package
Compiling the classes in a package can be a bit tricky unless you are clear on how you go about it. We
will illustrate what you need to do assuming you are using the JDK under Microsoft Windows. The path
to the package directory has to be explicitly made known to the compiler, even when the current
directory is the one containing the package. If we have stored the Geometry package source files in the
directory with the path C:\Beg Java Stuff\Geometry , then the path to the Geometry directory
is C:\Beg Java Stuff . You can tell the compiler about this path using the -classpath option on
the command line. Assuming that the current directory is the Geometry directory, we could compile
the Line.java source file with the command:
C:\JavaStuff\Geometry> javac -classpath "C:\Beg Java Stuff" Line.java
This will result in both the Line.java and Point.java files being compiled, since Line.java
refers to the other class. Since the directory in the path contains spaces, we have to enclose the path
between double quotes.
Accessing a Package
How you access a package, when you are compiling a program that uses the package, depends on where
you have put it. There are a couple of options here. The first, but not the best, is to leave the .class
files for the classes in the package in the directory with the package name.
Search WWH ::




Custom Search