Java Reference
In-Depth Information
name as the package, that is, Geometry . Note the use of the public keyword in the definition of the Line
class. This makes the class accessible generally. If you omit the public keyword from the class definition,
the class is accessible only from methods in classes that are in the Geometry package.
Note that you also need to declare the constructors and methods in the class as public if you want them
to be accessible from outside of the package. I return to this in more detail a little later in this chapter.
Packages and the Directory Structure
Packages are actually a little more complicated than they appear at first sight, because a package is intim-
ately related to the directory structure in which it is stored. You already know that the definition of a class
with the name ClassName must be stored in a file with the name ClassName.java , and that all the files for
classes within a package PackageName must be included in a directory with the name PackageName . You
can compile the source for a class within a package and have the .class file that is generated stored in a
different directory, but the directory name must still be the same as the package name.
As you are aware from the existence of the java.lang package that contains the String class, a package
can have a composite name that is a combination of two or more simple names. You can specify a pack-
age name as any sequence of names separated by periods. For example, you might have developed several
collections of classes dealing with geometry, perhaps one that works 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 files containing the classes in the Geometry.Shapes3D packages are expected to be
in the directory Shapes3D and the files containing the classes in the Geometry.Shapes2D packages are ex-
pected to be in the directory Shapes2D . Both of these directories must be subdirectories of a directory with
the name Geometry . In general, you can have as many names as you like separated by periods to identify a
package, but the package name must reflect the directory structure in which the package is stored.
Compiling a Package
Compiling the classes in a package can be a bit tricky unless you are clear on how you go about it. I'll
describe what you need to do assuming you are using the JDK under Microsoft Windows. The path to the
package directory must be explicitly made known to the compiler in the value that is set for CLASSPATH ,
even when the current directory is the one containing the package. The easiest way to specify CLASSPATH is
by using the -classpath option when you invoke the compiler.
The path to the package directory is the path to the directory that contains the package directory, and
therefore does not include the package directory itself. For example, if you have stored the source files for
classes that are in the Geometry package in the directory with the path C:\Beg Java Stuff\Geometry then
the path to the Geometry directory is C:\Beg Java Stuff . Many beginners mistakenly specify the path as
C:\Beg Java Stuff\Geometry , in which case the package is not found.
Search WWH ::




Custom Search