Packages and Compilation Units
A package is a name for a group of related classes and interfaces. In Chapter 3, we discussed how Java uses package names to locate classes during compilation and at runtime. In this sense, packages are somewhat like libraries; they organize and manage sets of classes. Packages provide more than just source-code-level organization. They create an additional level of scope for their classes and the variables and methods within them. We’ll talk about the visibility of classes later in this section. In the next section, we discuss the effect that packages have on access to variables and methods among classes.
Compilation Units
The source code for Java classes is organized into
compilation units. A simple compilation unit
contains a single class definition and is named for that class. The
definition of a class named MyClass,
for instance, could appear in a file named
MyClass.java. For most of us, a compilation unit is
just a file with a .java extension, but theoretically in
an IDE, it could be an arbitrary entity. For brevity, we’ll refer to a
compilation unit simply as a file.
The division of classes into their own files is important because the Java compiler assumes much of the responsibility of a make or build utility. The compiler relies on the names of source files to find and compile dependent classes. It’s possible to put more than one class definition into a single file, but there are some restrictions that we’ll discuss shortly.
A class is declared ...