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

Unconditional opens: Opening Packages to All Modules

Learn how to open a package to any module without restriction using the simple opens directive.

Unconditional opens: Opening Packages to All Modules

What you'll learn: How to use the simple opens directive to make a package's types accessible for deep reflection to any module at runtime.

The Simple opens Directive

When you want to grant unrestricted reflective access to a package without limiting which modules can access it, you use the unconditional opens directive. This is the simplest form of the opens statement:

module myapp {
    opens com.myapp.entities;
}

This single line allows any module in the system to use reflection on all types within com.myapp.entities—reading private fields, invoking private methods, and manipulating objects at runtime.

Why Use Unconditional opens?

Think of it like leaving your workshop door unlocked for authorized tool users. You're not advertising what's inside (no compile-time access), but any framework that needs deep access can enter freely.

Common scenarios include:

  • Persistence frameworks (Hibernate, JPA) need to access private fields in entity classes
  • Serialization libraries (Jackson, Gson) must read/write private state
  • Dependency injection frameworks (Spring, CDI) require constructor and field access

Unrestricted Means Unrestricted

Unlike qualified opens (which we'll see later), the unconditional form doesn't specify which modules can reflect. All modules gain deep reflective access, including third-party libraries you might add later. This maximizes compatibility but offers less control.

The package remains strongly encapsulated for normal access—you still cannot import types from an opened-but-not-exported package.

Key Takeaway:
Use opens <package> to grant reflective access to a package for all modules without exposing it for compile-time use.