---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 9 Edition"
java_version: 9
chapter: "Chapter 7. Packages and Modules"
section: "7.4"
heading: "7.4. Package Declarations"
parent: "Chapter 7. Packages and Modules"
source: https://docs.oracle.com/javase/specs/jls/se9/html/jls-7.html#jls-7.4
id: "7.4"
url: https://jvm.obfuscated.software/jls/se9/7.4.html
raw: https://jvm.obfuscated.software/jls/se9/7.4.md
---

# 7.4. Package Declarations

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](6.2.md)) 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](6.7.md)).

The scope and shadowing of a package declaration is specified in [§6.3](6.3.md) and [§6.4](6.4.md).

The rules for annotation modifiers on a package declaration are specified in [§9.7.4](9.7.4.md) and [§9.7.5](9.7.5.md).

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

An ordinary compilation unit that has no `package` 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](7.7.5.md)), 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](7.3.md)).
- 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](7.3.md)). 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](10.1.md)) 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 "really" observable in some module on the system. It is not useful to think that package P is "technically" observable just because a subpackage P.Q is "technically" observable in some module. For example, suppose P.Q is "technically" observable in module M1 and P.R is "technically" observable in module M2; then, P is "technically" observable, but in M1 or M2 or both? During the compilwation of some module N that requires M1 only, it matters that P.Q is "really" observable, and it does not matter that P is "technically" 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 `M` contains a declaration of the package, and `M` does not read any other module that exports the package to `M`.
- No ordinary compilation unit associated with `M` contains a declaration of the package, and `M` reads exactly one other module that exports the package to `M`.
