Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^296) | Object-Oriented Software Design and Implementation
(^3) In addition to following the naming conventions, most Java systems require that you specify that the
compiler should include this directory among the set that it examines when it compiles your code.
With an integrated development environment, this specification can be as simple as dragging the file
into a window that lists all of the files for the project. In a command-line environment, you may have
to enter an operating system command that defines a value called a class path. Check the documenta-
tion for your system, or ask your instructor whether something like this needs to be done.


Packages with Multiple Compilation Units


Each Java compilation unit is stored in its own file. Java systems name the file using a com-
bination of the package name and the name of the publicclass in the compilation unit. Java
restricts us to having a single publicclass in a file so that it can use file names to locate all
publicclasses. Thus, a package with multiple publicclasses must be implemented with mul-
tiple compilation units, each placed in a separate file.
Using multiple compilation units has the further advantage that it provides us with
more flexibility in developing the classes of a package. Team programming projects would
be very cumbersome if Java made multiple programmers share a single package file.
We split a package among multiple files simply by placing its members into separate com-
pilation units with the same package name. For example, we can create one file containing
the following code (the... between the braces represents the code for each class):
packagesomeName;
public classOne { ... }
classTwo { ... }
A second file contains the following code:
packagesomeName;
classThree { ... }
public classFour { ... }
The result is that the package someNamecontains four classes, all of which have access to
each other’s nonprivate members. Two of the classes,Oneand Four,are public, and so are
available to be imported by user code.
Many programmers simply place every class in its own compilation unit. Others gather
the nonpublic classes into one unit, separate from the publicclasses. How you organize your
packages is up to you, but you should use a consistent approach to make it easy to find the
members of the package among all of its files.
How does the Java compiler find these separate pieces and put them together? The an-
swer is that the compiler requires all of the compilation unit files for a package to reside in
a single directory or folder. For our preceding example, a Java system would store the source
code in files called One.javaand Four.javain a directory called someName.
Splitting a package among multiple files has one other benefit. Each compilation unit can
have its own set of import declarations. Thus, if the classes in a package need to use differ-
ent sets of imported classes, you can place them in separate compilation units, each with just
the import declarations that are required.^3
Free download pdf