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

# 7.3. Compilation Units

*CompilationUnit* is the goal symbol ([§2.1](2.1.md)) for the syntactic grammar ([§2.3](2.3.md)) of Java programs. It is defined by the following production:

```
CompilationUnit:
  OrdinaryCompilationUnit
  ModularCompilationUnit

OrdinaryCompilationUnit:
  [PackageDeclaration] {ImportDeclaration} {TopLevelClassOrInterfaceDeclaration}

ModularCompilationUnit:
  {ImportDeclaration} ModuleDeclaration
```

An *ordinary compilation unit* consists of three parts, each of which is optional:

- A `package` declaration ([§7.4](7.4.md)), giving the fully qualified name ([§6.7](6.7.md)) of the package to which the compilation unit belongs.

  A compilation unit that has no `package` declaration is part of an unnamed package ([§7.4.2](7.4.2.md)).

- `import` declarations ([§7.5](7.5.md)) that allow classes and interface from other packages, and `static` members of classes and interfaces, to be referred to using their simple names.

- Top level declarations of classes and interfaces ([§7.6](7.6.md)).

A *modular compilation unit* consists of a `module` declaration ([§7.7](7.7.md)), optionally preceded by `import` declarations. The `import` declarations allow classes and interfaces from packages in this module and other modules, as well as `static` members of classes and interfaces, to be referred to using their simple names within the `module` declaration.

Every compilation unit implicitly imports every `public` class or interface declared in the predefined package `java.lang`, as if the declaration `import java.lang.*;` appeared at the beginning of each compilation unit immediately after any `package` declaration. As a result, the names of all those classes and interfaces are available as simple names in every compilation unit.

The host system determines which compilation units are *observable*, except for the compilation units in the predefined package `java` and its subpackages `lang` and `io`, which are all always observable.

Each observable compilation unit may be *associated* with a module, as follows:

- The host system may determine that an observable ordinary compilation unit is associated with a module chosen by the host system, except for (i) the ordinary compilation units in the predefined package `java` and its subpackages `lang` and `io`, which are all associated with the `java.base` module, and (ii) any ordinary compilation unit in an unnamed package, which is associated with a module as specified in [§7.4.2](7.4.2.md).
- The host system must determine that an observable modular compilation unit is associated with the module declared by the modular compilation unit.

The observability of a compilation unit influences the observability of its package ([§7.4.3](7.4.3.md)), while the association of an observable compilation unit with a module influences the observability of that module ([§7.7.6](7.7.6.md)).

When compiling the modular and ordinary compilation units associated with a module `M`, the host system must respect the dependences specified in `M`'s declaration. Specifically, the host system must limit the ordinary compilation units that would otherwise be observable, to only those that are *visible to `M`*. The ordinary compilation units that are visible to `M` are the observable ordinary compilation units associated with the modules that are *read by `M`*. The modules read by `M` are given by the result of *resolution*, as described in the `java.lang.module` package specification, with `M` as the only root module. The host system must perform resolution to determine the modules read by `M`; it is a compile-time error if resolution fails for any of the reasons described in the `java.lang.module` package specification.

The readability relation is reflexive, so `M` reads itself, and thus all of the modular and ordinary compilation units associated with `M` are visible to `M`.

The modules read by `M` drive the packages that are uniquely visible to `M` ([§7.4.3](7.4.3.md)), which in turn drives both the top level packages in scope and the meaning of package names for code in the modular and ordinary compilation units associated with `M` ([§6.3](6.3.md), [§6.5.3](6.5.3.md), [§6.5.5](6.5.5.md)).

The rules above ensure that package and type names used in annotations in a modular compilation unit (in particular, annotations applied to the module declaration) are interpreted as if they appeared in an ordinary compilation unit associated with the module.

Classes and interfaces declared in different ordinary compilation units can refer to each other, circularly. A Java compiler must arrange to compile all such classes and interfaces at the same time.
