Quick revision
The short answer
exports makes the public types of a package, and their public and protected members, accessible to other modules at both compile time and run time. opens grants run-time deep reflective access to all members of all types in the package, including private ones, but no compile-time access. The qualified forms, exports p to m and opens p to m, limit that access to the named modules, while the unqualified forms apply to every module, and an open module declaration opens every package in the module for reflection.
What to remember
In this answer, we saw that exports gives other modules compile-time and run-time access to a package's public types and their public and protected members, while opens gives run-time deep reflection on all members, including private ones, without compile-time access. The qualified forms, exports p to m and opens p to m, restrict that access to named modules, the unqualified forms grant it to all modules, and an open module opens every package for reflection while still using exports for compile-time access.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
exports makes the public types of a package, and their public and protected members, accessible to other modules at both compile time and run time. opens grants run-time deep reflective access to all members of all types in the package, including private ones, but no compile-time access. The qualified forms, exports p to m and opens p to m, limit that access to the named modules, while the unqualified forms apply to every module, and an open module declaration opens every package in the module for reflection.
Why this question is asked
Before Java 9, public meant visible to everyone, and private meant private until someone called setAccessible(true). The module system split that old meaning into separate decisions: who may compile against a package, and who may reflect into it. Interviewers ask this because frameworks depend on the second decision every day, and many module errors in practice come from mixing the two up. In this answer, we will recap strong encapsulation, look at what exports allows and what it does not, then at what opens allows, see the qualified forms, and finish with open modules and how to choose.
1A Quick Recap: Strong Encapsulation
In a named module, a package is encapsulated unless the descriptor says otherwise. Code in other modules cannot use its types, even public ones, and cannot reflect on them. This is called strong encapsulation, and it is the main promise of the module system: a module's internals really are internal.
There are two ways to relax it, and they answer different questions. exports answers: may other modules use this package's API, write code against it and call it? opens answers: may other modules look inside this package at run time with reflection, including at private fields and methods? Frameworks such as Hibernate, Jackson and Spring mostly need the second, while ordinary consumers of a library need the first.
2Exports: Compile-Time and Run-Time Access to the API
exports p makes the public types in package p accessible, with their public members and protected members (to subclasses). Other modules can compile against them and call them, and they can use ordinary reflection on those public members at run time. What exports does not give is access to private members by reflection, even for a module that can see the type.
Notice the difference between the two reflective calls. Calling a public method by reflection is fine, because exports already made that method accessible. Making a private field accessible is deep reflection, and exports does not allow it. Before Java 9 that last line always worked, which is why upgrading old applications revealed so many hidden uses of private reflection.
3Opens: Run-Time Deep Reflection
opens p allows other modules to reflect on every member of every type in p at run time, private ones included, so setAccessible(true) succeeds. But opens says nothing about compile time: a package that is opened but not exported cannot be imported by other modules' source code.
| Directive | Compile-time access | Run-time public members | Run-time private members |
|---|---|---|---|
| Neither | No | No | No |
| exports p | Yes | Yes | No |
| opens p | No | Yes, by reflection | Yes, deep reflection |
| exports p + opens p | Yes | Yes | Yes |
This is exactly what a framework needs. Hibernate does not compile against our entity classes; it discovers them at run time and reads and writes their private fields. Jackson and Spring do the same with DTOs and beans. So opens is the directive for frameworks, and exports is the directive for API consumers. A package can have both when it needs both.
4Qualified and Unqualified Forms
Each directive has two forms. The unqualified form applies to every module: exports p or opens p. The qualified form names the modules that get the access: exports p to m1, m2 or opens p to m1. Other modules see the package as if the directive were not there.
Qualified forms follow the principle of least privilege: we grant exactly the access a known consumer needs and nothing more. A qualified export is useful between modules owned by the same team, where one module needs another's internals but the rest of the world should not. A qualified open is the usual way to serve a framework. The trade-off is maintenance: the descriptor now names framework modules, so if a framework changes its module name between major versions, the descriptor must change too. The JDK itself uses qualified exports heavily, letting its own modules share internal packages that applications cannot use.
5Open Modules
Finally, the whole module can be declared open, which opens every package in it, unqualified, for deep reflection at run time.
An open module keeps compile-time encapsulation, since only exported packages can be compiled against, but gives up run-time encapsulation entirely. It is a pragmatic choice for applications full of framework-managed classes, where listing every package and framework would be tedious and fragile, and a common first step when migrating such an application to the module path. For libraries it is usually too generous, because it lets any module reflect into internal state the library's authors meant to keep private.
6Follow-up questions
Can we open a package without changing the descriptor?
Yes, at launch: --add-opens com.acme.shop/com.acme.shop.model=ALL-UNNAMED opens a package to all class path code, and --add-exports does the same for exports. They are meant for migration and tooling, not as a permanent design.
Why does code on the class path still reflect into some JDK internals?
It mostly cannot any more. Since JDK 16 the JDK's internal packages are strongly encapsulated by default and since JDK 17 the old --illegal-access escape hatch is gone, so class path code needs explicit --add-opens for deep reflection into JDK internals.
Does opens imply exports for public types?
Only at run time, through reflection: an opened package's public members can be used reflectively. It still cannot be compiled against, so for a normal API we always need exports.
7Conclusion
In this answer, we saw that exports gives other modules compile-time and run-time access to a package's public types and their public and protected members, while opens gives run-time deep reflection on all members, including private ones, without compile-time access. The qualified forms, exports p to m and opens p to m, restrict that access to named modules, the unqualified forms grant it to all modules, and an open module opens every package for reflection while still using exports for compile-time access.
Open with
The two directives answer different questions. exports says other modules may use this package's API: its public types and their public and protected members are accessible at compile time and at run time, including ordinary reflection on public members.
Then, if they want more
But exports does not allow deep reflection, so setAccessible on a private field from another module throws InaccessibleObjectException. opens is about run time only: it allows deep reflection on every member of every type in the package, private ones included, but it gives no compile-time access, so other modules cannot import the package.
That is why we use exports for API consumers and opens for frameworks like Hibernate, Jackson and Spring, which reflect on our entities and DTOs at run time. A package can have both.
Each has a qualified form, exports p to m or opens p to m, which grants the access only to the named modules, for example opening entities only to Hibernate's module, and an unqualified form, which grants it to every module. I prefer the qualified form for frameworks because it is least privilege.
Finally, declaring the whole module as open module opens every package for reflection, while exports still control compilation; it is convenient for framework-heavy applications, but too generous for libraries.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 16, Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the difference between exports and opens, and between qualified and unqualified...": https://scrivio-interview-library.netlify.app/java/modules/1c893befdd/