Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

Explain the difference between exports and opens in module-info for reflection. Why does a JPA or Jackson module often need opens?

exports makes a package's public types and their public and protected members accessible to other modules at compile time and at run time. opens allows deep reflection on a package at run time only, including setAccessible on private fields, methods and constructors. Hibernate and Jackson often read private fields or call private constructors of our model classes, so the package holding those classes must be opened to them, ideally with a qualified opens such as opens com.acme.model to com.fasterxml.jackson.databind. An open module opens all its packages at once.

Why this question is asked

For years, any library could reach into any class's private fields, and frameworks were built on that freedom. The module system, introduced in Java 9, made that access something a module grants on purpose. Most applications still run on the class path, where everything is open, so the difference only bites when a team moves its code into named modules and the JSON mapper or the ORM suddenly fails. Interviewers ask this question to check whether we understand the two kinds of access and why frameworks need the second. In this answer, we will recap modules and access, compare exports and opens, see the failure without opens, narrow the grant with qualified opens, and look at open modules.

1A Quick Recap of Modules and Access

A module is a named group of packages with a module-info.java file that states what it needs and what it offers. requires names the modules it depends on. By default, nothing in a module is accessible from outside it, not even public classes: a package is hidden unless the module says otherwise.

There are two different things another module might want. The first is ordinary use: compiling against our public classes and calling their public methods, the way any library is used. The second is deep reflection: using the reflection API to look inside our classes, call setAccessible(true) and read or write private members. Frameworks that map data onto objects need the second, because they work with fields and constructors that we never meant to be part of a public API. The module system keeps the two separate, with exports for the first and opens for the second.

2Exports Versus opens

exports com.acme.model makes the public types of that package accessible to other modules, at compile time and at run time, with their public and protected members. Private members stay inaccessible, even by reflection: setAccessible(true) on a private field throws.

opens com.acme.model grants deep reflective access at run time to every type in the package and every member, private ones included. It does not make the package available at compile time: another module cannot write code that imports these types unless the package is also exported. That fits the frameworks' needs exactly, because Jackson and Hibernate never compile against our model classes; they discover them at run time.

Java
module com.acme.orders {
requires com.fasterxml.jackson.databind;
requires org.hibernate.orm.core;
exports com.acme.orders.api; // others compile against our public API
opens com.acme.orders.model; // anyone may reflect deeply, run time only
exports com.acme.orders.dto;
opens com.acme.orders.dto to com.fasterxml.jackson.databind; // only Jackson may reflect deeply
}
What each directive allows
DirectiveCompile timeRun time, public membersRun time, private members
(nothing)NoNoNo
exports pYesYesNo
opens pNoYes, by reflectionYes, by deep reflection
exports p and opens pYesYesYes, by deep reflection

A package can be both exported and opened, as dto is here, when other modules compile against its types and a framework also needs to reach their private fields.

3What Fails Without opens

Take a Customer entity with private fields and no setters, a common style when the ORM uses field access, in a module that only exports its model package.

Java
// module com.acme.model { exports com.acme.model; }
public class Customer {
private Long id;
private String name;
protected Customer() { } // for the ORM and the JSON mapper
public Customer(String name) { this.name = name; }
public String name() { return name; }
}
new ObjectMapper().readValue("{\"name\":\"Ana\"}", Customer.class);
// java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.String
// com.acme.model.Customer.name accessible: module com.acme.model does not
// "opens com.acme.model" to module com.fasterxml.jackson.databind
// (Jackson usually wraps this in its own exception; the cause carries this message)

Notice that exports was not enough. Jackson can see the class, but to set the private name field, or to call the protected no-argument constructor, it must call setAccessible(true), and that requires the package to be opened to Jackson's module. Hibernate fails the same way when it reads and writes entity fields directly or instantiates entities through a non-public constructor. Adding public setters and a public constructor would avoid deep reflection for Jackson, but it would weaken our design just to satisfy the framework, so opening the package is usually the better choice.

4Narrowing the Grant With Qualified opens

An unqualified opens lets any module reflect into the package. A qualified opens names the modules that may do so, which keeps the exposure as small as the need.

Java
module com.acme.model {
requires com.fasterxml.jackson.databind;
requires org.hibernate.orm.core;
exports com.acme.model;
opens com.acme.model to com.fasterxml.jackson.databind, org.hibernate.orm.core;
}

Now Jackson and Hibernate can read the private fields, and any other library cannot, which is the point of strong encapsulation: the module states exactly who may look inside. Exports can be qualified in the same way, exports com.acme.internal to com.acme.web, to share a package only with friendly modules. One practical caveat is that the module names must be the frameworks' real module names, which we check in each library's documentation or jar manifest; an automatic module, a plain jar on the module path, takes its name from the Automatic-Module-Name manifest entry or from the jar file name.

5Open Modules and the Class Path

If a module exists mainly to hold model classes that frameworks inspect, listing every package can be tedious. Declaring the whole module open, open module com.acme.model { ... }, opens all its packages for deep reflection at run time, while exports still controls what others can compile against. It is a reasonable choice for such modules, but it gives up the fine-grained control, so it is better avoided for modules with internal logic.

Finally, most Spring Boot applications still run on the class path, where all our code sits in the unnamed module. The unnamed module is open, and it can read every other module, so our own classes need no opens at all. The rules in this answer apply when our code is in named modules, and to the JDK's own modules, which do not open their internals to anybody by default.

Java
open module com.acme.model { // every package is open for deep reflection
requires com.fasterxml.jackson.databind;
exports com.acme.model; // still decides what others compile against
}

6Follow-up questions

Does opens also give access to public members?

Yes, at run time by reflection. An opened package's public types and members can be accessed reflectively even if the package is not exported, and private members can be accessed after setAccessible(true). What opens does not give is compile-time access; for that the package must be exported.

How does a framework get access without asking for setAccessible?

The module can pass the framework a Lookup created inside the module, and the framework can call MethodHandles.privateLookupIn(Customer.class, thatLookup), or use the Lookup directly to create method and var handles. privateLookupIn still requires the package to be open to the framework's module, so opens remains the switch.

Can we fix a missing opens without changing module-info?

Yes, with the command-line option --add-opens com.acme.model/com.acme.model=com.fasterxml.jackson.databind, which is useful for a quick test or for third-party modules we cannot change. For our own modules, the module-info is the right place, because the grant then lives with the code and is reviewed with it.

7Conclusion

In this answer, we saw that exports gives other modules compile-time and run-time access to a package's public types and public members, while opens allows deep reflection at run time, including setAccessible on private members. Hibernate and Jackson read private fields and call non-public constructors, so they need the model package opened to them, best with a qualified opens naming their modules. An open module opens every package at once, and on the class path the unnamed module is open already.

What to say in the interviewAbout 80 seconds aloud

Open with

In module-info, exports and opens control two different kinds of access. exports makes a package's public types, with their public and protected members, accessible to other modules at compile time and at run time.

Then, if they want more

So another module can import and call them, but it can't reach private members, even with reflection; setAccessible would throw InaccessibleObjectException. opens is about run time only: it allows deep reflection into the package, meaning setAccessible on private fields, methods and constructors, but it doesn't make the package available at compile time. A package can be both.

JPA providers like Hibernate and JSON mappers like Jackson often need opens because they read and write private fields, for example with field access on entities, and call protected or private no-argument constructors. With only exports, Jackson fails with an InaccessibleObjectException saying the module does not open the package to com.fasterxml.jackson.databind.

I'd use a qualified opens, opens com.acme.model to com.fasterxml.jackson.databind and the Hibernate module, so only those frameworks can look inside. For a module that only holds data classes, declaring an open module opens every package at once, at the cost of control.

Worth noting that on the class path everything is in the unnamed module, which is open, so this mostly bites when you move to named modules.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
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.
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 in module-info for reflection. Why does...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/3f5600e143/