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

# Chapter 7. Packages and Modules

Programs are organized as sets of packages. The members of a package ([§7.1](7.1.md)) are class and interface types, which are declared in compilation units of the package, and subpackages, which may contain compilation units and subpackages of their own.

Each package has its own set of names for types, which helps to prevent name conflicts. The naming structure for packages is hierarchical.

If a set of packages is sufficiently cohesive, then the packages may be grouped into a module. A module categorizes some or all of its packages as exported, which means their types may be accessed from code outside the module. If a package is not exported by a module, then only code inside the module may access its types. Furthermore, if code in a module wishes to access the packages exported by another module, then the first module must explicitly depend on the second module. Thus, a module controls how its packages use other modules (by specifying dependences) and controls how other modules use its packages (by specifying which of its packages are exported).

Modules and packages may be stored in a file system or in a database ([§7.2](7.2.md)). Modules and 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 module and type declarations easily.

Code in a compilation unit automatically 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`.

A top level type is accessible ([§6.6](6.6.md)) outside the package that declares it only if the type is declared `public`. A top level type is accessible outside the module that declares it only if the type is declared `public` and is a member of an exported package. A type that is declared `public` but is not a member of an exported package is accessible only to code inside the module.

For small programs and casual development, a package can be unnamed ([§7.4.2](7.4.2.md)) 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 development groups happened to pick the same package name and these packages were later to be used in a single program.

**Sections:**

- [7.1. Package Members](7.1.md)
- [7.2. Host Support for Modules and Packages](7.2.md)
- [7.3. Compilation Units](7.3.md)
- [7.4. Package Declarations](7.4.md)
- [7.5. Import Declarations](7.5.md)
- [7.6. Top Level Type Declarations](7.6.md)
- [7.7. Module Declarations](7.7.md)
