Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

The keyword opens

The keyword opens: The opens directive opens one module package for deep run-time reflection, optionally only to modules named after to.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

opens

The opens directive opens one module package for deep run-time reflection, optionally only to modules named after to.

Use qualified opens for a serializer or DI container that needs reflective access to non-public members.

opens does not expose types for ordinary compilation; exports and opens solve different needs, and the named package must belong to the module.

Unqualified opens applies to every module, qualified opens only to its targets, and member or lookup checks can still matter.

source=demo.internal, qualified=true, and targets=[framework.core] demonstrate qualified opens.

Examples

import java.lang.module.*; public class Main { public static void main(String[] a){var d=ModuleDescriptor.newModule("demo.app").opens("demo.internal",java.util.Set.of("framework.core")).build();var o=d.opens().iterator().next();System.out.println("source="+o.source());System.out.println("qualified="+o.isQualified());System.out.println("targets="+new java.util.TreeSet<>(o.targets()));} }

Result

source=demo.internal
qualified=true
targets=[framework.core]

source=demo.internal, qualified=true, and targets=[framework.core] demonstrate qualified opens.

Sources