Course contentsShow
← Java
Lesson 2468 of 2,87047. Java Platform Module System (JPMS)Free lesson

Module Descriptors and Accessibility Rules

Learn how module-info.java controls what is accessible outside the module by default (nothing unless exported).

Module Descriptors and Accessibility Rules

What you'll learn: How the module-info.java file controls what gets exposed outside your module and why everything is hidden by default.

The Default: Everything Is Private

In the Java Platform Module System (JPMS), the module-info.java file serves as the gatekeeper for your module. By default, nothing inside your module is accessible to other modules—not even public classes. This is called strong encapsulation.

Think of your module as a house with all the curtains drawn. Even if you're shouting inside (public classes), no one outside can hear you unless you open a window (exports declaration).

Accessibility Without Exports

Without an exports statement, your module's packages are:

  • Invisible to other modules at compile time
  • Inaccessible at runtime, even via reflection (by default)

This means external code cannot:

  • Import your classes
  • Call your methods
  • Access your fields

Even if a class is declared public, it remains module-private unless explicitly exported.

How Module Descriptors Control Access

The module-info.java descriptor acts as a contract between your module and the outside world. It declares:

  • What you provide (exports)
  • What you need (requires)
  • What services you offer or consume

Without any exports directives, your module becomes a black box—completely self-contained and isolated from external access. This is intentional: it forces you to explicitly decide what constitutes your public API.

Why This Matters

This default-closed approach prevents accidental dependencies on internal implementation details, making your codebase more maintainable and allowing internal refactoring without breaking external consumers.

Key Takeaway: In JPMS, your module-info.java file enforces that nothing is accessible outside the module by default—you must explicitly export packages to make them available to other modules.