Java Reference
In-Depth Information
18.4. Package Contents
Packages should be designed carefully so that they contain only function-
ally related classes and interfaces. Classes in a package can freely access
one another's non-private members. Protecting class members is inten-
ded to prevent misuse by classes that have access to internal details of
other classes. Anything not declared
private
is available to all types in
the package, so unrelated classes could end up working more intimately
than expected with other classes.
Packages should also provide logical groupings for programmers who are
looking for useful interfaces and classes. A package of unrelated classes
makes the programmer work harder to figure out what is available. Lo-
gical grouping of classes helps programmers reuse your code because
they can more easily find what they need. Including only related, co-
herent sets of types in a package also means that you can use obvious
names for types, thereby avoiding name conflicts.
Packages can be nested inside other packages. For example,
java.lang
is
a nested package in which
lang
is nested inside the larger
java
package.
The
java
package contains only other packages. Nesting allows a hier-
archical naming system for related packages.
For example, to create a set of packages for adaptive systems such as
neural networks and genetic algorithms, you could create nested pack-
ages by naming the packages with dot-separated names:
package adaptive.neuralNet;
A source file with this declaration lives in the
adaptive.neuralNet
package,
which is itself a subpackage of the
adaptive
package. The
adaptive
pack-
age might contain classes related to general adaptive algorithms, such
as generic problem statement classes or benchmarking. Each package
deeper in the hierarchysuch as
adaptive.neuralNet
or
adapt-
ive.genetic
would contain classes specific to the particular kind of adapt-
ive algorithm.