A package declaration appears within an ordinary compilation unit to indicate the package to which the compilation unit belongs.
7.4.1. Named Packages
A package declaration in an ordinary compilation unit specifies the name (§6.2) of the package to which the compilation unit belongs.
PackageDeclaration:
{PackageModifier} package Identifier {. Identifier} ;
PackageModifier:
Annotation
The package name mentioned in a package declaration must be the fully qualified name of the package (§6.7).
The scope and shadowing of a package declaration is specified in §6.3 and §6.4.
The rules concerning annotation modifiers for a package declaration are specified in §9.7.4 and §9.7.5.
At most one annotated package declaration is permitted for a given package.
The manner in which this restriction is enforced must, of necessity, vary from implementation to implementation. The following scheme is strongly recommended for file-system-based implementations: The sole annotated package declaration, if it exists, is placed in a source file called package-info.java in the directory containing the source files for the package. This file does not contain the source for a class called package-info; indeed it would be illegal for it to do so, as package-info is not a legal identifier. Typically package-info.java contains only a package declaration, preceded immediately by the annotations on the package. While the file could technically contain the source code for one or more classes with package access, it would be very bad form.
It is recommended that package-info.java, if it is present, take the place of package.html for javadoc and other similar documentation generation systems. If this file is present, the documentation generation tool should look for the package documentation comment immediately preceding the (possibly annotated) package declaration in package-info.java. In this way, package-info.java becomes the sole repository for package-level annotations and documentation. If, in future, it becomes desirable to add any other package-level information, this file should prove a convenient home for this information.
7.4.2. Unnamed Packages
A compact compilation unit, or an ordinary compilation unit that has no package declaration but has at least one other kind of declaration, is part of an unnamed package.
Unnamed packages are provided by the Java SE Platform principally for convenience when developing small or temporary applications or when just beginning development.
An unnamed package cannot have subpackages, since the syntax of a package declaration always includes a reference to a named top level package.
An implementation of the Java SE Platform must support at least one unnamed package. An implementation may support more than one unnamed package, but is not required to do so. Which ordinary compilation units are in each unnamed package is determined by the host system.
The host system must associate ordinary compilation units in an unnamed package with an unnamed module (§7.7.5), not a named module.
Example 7.4.2-1. Unnamed Package
The compilation unit:
class FirstCall {
public static void main(String[] args) {
System.out.println("Mr. Watson, come here. "
+ "I want you.");
}
}
defines a very simple compilation unit as part of an unnamed package.
In implementations of the Java SE Platform that use a hierarchical file system for storing packages, one typical strategy is to associate an unnamed package with each directory; only one unnamed package is observable at a time, namely the one that is associated with the "current working directory". The precise meaning of "current working directory" depends on the host system.
7.4.3. Package Observability and Visibility
A package is observable if and only if at least one of the following is true:
- An ordinary compilation unit containing a declaration of the package is observable (§7.3).
- A subpackage of the package is observable.
The packages java, java.lang, and java.io are always observable.
One can conclude this from the rule above and from the rules of observable compilation units, as follows. The predefined package java.lang declares the class Object, so the compilation unit for Object is always observable (§7.3). Hence, the java.lang package is observable, and the java package also. Furthermore, since Object is observable, the array type Object[] implicitly exists. Its superinterface java.io.Serializable (§10.1) also exists, hence the java.io package is observable.
A package is visible to a module M if and only if an ordinary compilation unit containing a declaration of the package is visible to M.
Package visibility is meant to imply that a package is observable in a useful way to a given module. It is generally not useful to know that package P is observable merely because a subpackage P.Q is observable. For example, suppose P.Q is observable (in module M1) and P.R is observable (in module M2); then, P is observable, but where? In M1, or M2, or both? The question is redundant; during compilation of module N that requires only M1, it matters that P.Q is observable, but it does not matter that P is observable.
A package is uniquely visible to a module M if and only if one of the following holds:
- An ordinary compilation unit associated with
Mcontains a declaration of the package, andMdoes not read any other module that exports the package toM. - No ordinary compilation unit associated with
Mcontains a declaration of the package, andMreads exactly one other module that exports the package toM.