---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 7. Packages"
section: "7.3"
heading: "7.3. Compilation Units"
parent: "Chapter 7. Packages"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.3
id: "7.3"
url: https://jvm.obfuscated.software/jls/se8/7.3.html
raw: https://jvm.obfuscated.software/jls/se8/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 productions:

```
CompilationUnit:
  [PackageDeclaration] {ImportDeclaration} {TypeDeclaration}
```

A *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 types from other packages and `static` members of types to be referred to using their simple names.

- Top level type declarations ([§7.6](7.6.md)) of class and interface types.

Every compilation unit implicitly imports every `public` type name 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` statement. As a result, the names of all those types are available as simple names in every compilation unit.

All the compilation units of the predefined package `java` and its subpackages `lang` and `io` are always *observable*.

For all other packages, the host system determines which compilation units are observable.

The observability of a compilation unit influences the observability of its package ([§7.4.3](7.4.3.md)).

Types declared in different compilation units can depend on each other, circularly. A Java compiler must arrange to compile all such types at the same time.
