Java Reference
In-Depth Information
Chapter 7. Packages
Programs are organized as sets of packages. Each package has its own set of names for types,
which helps to prevent name conflicts.
A top level type is accessible (§ 6.6 ) outside the package that declares it only if the type is
declared public .
The naming structure for packages is hierarchical (§ 7.1 ). The members of a package are
class and interface types (§ 7.6 ) , which are declared in compilation units of the package, and
subpackages, which may contain compilation units and subpackages of their own.
A package can be stored in a file system or in a database (§ 7.2 ) . Packages that are stored in
a file system may have certain constraints on the organization of their compilation units to
allow a simple implementation to find classes easily.
A package consists of a number of compilation units (§ 7.3 ) . A compilation unit automatic-
ally has access to all types declared in its package and also automatically imports all of the
public types declared in the predefined package java.lang .
For small programs and casual development, a package can be unnamed (§ 7.4.2 ) or have a
simple name, but if code is to be widely distributed, unique package names should be chosen
using qualified names. This can prevent the conflicts that would otherwise occur if two de-
velopment groups happened to pick the same package name and these packages were later
to be used in a single program.
7.1. Package Members
The members of a package are its subpackages and all the top level class types (§ 7.6 , §8)
and top level interface types (§9) declared in all the compilation units (§ 7.3 ) of the package.
For example, in the Java SE platform API:
• The package java has subpackages awt , applet , io , lang , net , and util , but no com-
pilation units.
• The package java.awt has a subpackage named image , as well as a number of
compilation units containing declarations of class and interface types.
If the fully qualified name (§ 6.7 ) of a package is P , and Q is a subpackage of P , then P.Q is
the fully qualified name of the subpackage, and furthermore denotes a package.
Search WWH ::




Custom Search