Quick revision
The short answer
Strong encapsulation means code outside the JDK can no longer reach the JDK's internal packages, such as sun.* and jdk.internal.*, or use reflection to open private members of JDK classes, unless the access is opened explicitly with --add-opens or --add-exports. Java 9 introduced it with a permissive default, Java 16 made denial the default, and Java 17 removed the --illegal-access option altogether, so it can no longer be relaxed globally. Critical internal APIs such as sun.misc.Unsafe remain available through the jdk.unsupported module.
What to remember
In this answer, we saw that strong encapsulation adds a module boundary on top of access modifiers: only exported packages are usable, and only opened packages allow deep reflection. Java 9 enforced it gently with a permissive default and a warning, Java 16 made denial the default, and Java 17 removed --illegal-access, so the only remaining exceptions are per-package --add-opens and --add-exports flags. Critical APIs like Unsafe stayed reachable through jdk.unsupported while replacements arrived, and the whole effort exists so the JDK can evolve its internals without breaking our applications.
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.
Strong encapsulation means code outside the JDK can no longer reach the JDK's internal packages, such as sun.* and jdk.internal.*, or use reflection to open private members of JDK classes, unless the access is opened explicitly with --add-opens or --add-exports. Java 9 introduced it with a permissive default, Java 16 made denial the default, and Java 17 removed the --illegal-access option altogether, so it can no longer be relaxed globally. Critical internal APIs such as sun.misc.Unsafe remain available through the jdk.unsupported module.
Why this question is asked
Almost everyone who upgraded a service past Java 16 met an InaccessibleObjectException from some library. Interviewers ask about strong encapsulation because it explains those failures, shows whether we understand the module system's rules for reflection, and tells them whether we can fix such errors properly instead of by trial and error. In this answer, we will recap the difference between exported and internal packages, follow the timeline from Java 9 to 17, see the error and the precise flags that fix it, and explain why the JDK made this change.
1A Quick Recap: Public Is No Longer Enough
Before Java 9, the only boundary in Java was the access modifier. A public class in sun.misc or com.sun.org.apache.xerces.internal could be used by anyone, and with setAccessible(true) we could even read private fields of String or HashMap. The JDK documentation said these were internal, but nothing enforced it, so many libraries depended on them.
The module system added a second boundary. Each module lists the packages it exports, which are its public API. A public type in a package that is not exported is invisible to other modules at compile time and at run time. A module can also open a package, which additionally allows deep reflection: calling setAccessible(true) on private fields and methods. java.base exports java.lang and java.util, but it does not open them, and it neither exports nor opens jdk.internal.misc or sun.nio.ch.
2The Timeline From Java 9 to Java 17
Turning the boundary on at once would have broken a large part of the ecosystem, so the JDK did it in stages. Java 9 enforced encapsulation for code on the module path, but for the class path, where nearly all applications lived, it used the setting --illegal-access=permit. Access to packages that already existed in Java 8 still worked and printed a single warning on the first illegal access.
Java 16 (JEP 396) changed the default to deny, while still allowing teams to type --illegal-access=permit to get the old behaviour. Java 17 (JEP 403) removed the option: the JVM now prints a warning that the option is ignored and enforces encapsulation anyway. Since 17, the only way through is to name each package explicitly.
| Release | Default | Global escape hatch |
|---|---|---|
| Java 8 and earlier | Everything reachable | Not needed |
| Java 9 to 15 | permit: works, one warning | --illegal-access=deny to test early |
| Java 16 | deny | --illegal-access=permit still accepted |
| Java 17 and later | Enforced | Removed; only per-package flags |
3What the Failure Looks Like
Here is code that pokes into the internal array of a String, the kind of trick some serialisation and test libraries used to do.
Notice how precise the message is. It names the module (java.base), the package (java.lang) and the module asking for access, here the unnamed module, which is how the module system refers to everything on the class path. Those three pieces are exactly what the fix needs.
4Opening a Single Package With --add-opens and --add-exports
The two flags differ in what they grant. --add-exports makes the public types of an internal package visible, which is enough for code that calls a public internal class directly. --add-opens also allows deep reflection into private members, which is what setAccessible needs.
The format is module/package=target, and ALL-UNNAMED means all class path code. The manifest attributes only work for the main jar started with java -jar. These flags are deliberately narrow: we open one package to one target, so every exception is visible in our launch scripts and can be reviewed and removed later.
5Why sun.misc.Unsafe Still Works
Some internal APIs had no supported replacement in Java 9, and the most famous is sun.misc.Unsafe, used by high-performance libraries for off-heap memory and atomic operations. The JDK moved these critical APIs into a module called jdk.unsupported, which exports sun.misc and sun.reflect, so they keep working without flags. The plan has always been to remove them once replacements exist: VarHandle (Java 9) replaced most atomic uses, and the Foreign Function and Memory API (final in Java 22) replaces off-heap memory access. Unsafe's memory access methods are now deprecated for removal and print warnings on recent releases, so check your version.
6The Reason Behind All This
The goal is to let the JDK change its own internals without breaking applications. When libraries reach into private fields, every refactoring inside the JDK, such as compact strings changing String.value from char[] to byte[] in Java 9, can break them. Encapsulation also protects security and integrity: if any library can rewrite private fields of JDK classes, the JDK cannot guarantee that a final field is really final or that a security check cannot be bypassed. With the boundary enforced, the JDK developers can optimise and change internals freely, and we get upgrades that are safer by default.
7Follow-up questions
What is the difference between --add-exports and --add-opens?
--add-exports gives access to the public types and members of a package at compile time and run time, which is what code needs when it calls an internal class directly. --add-opens gives that plus deep reflection, so setAccessible(true) works on private and protected members. Use the narrower --add-exports when it is enough, because opening a package exposes everything in it.
Does strong encapsulation affect my own classes on the class path?
No. Everything on the class path is in the unnamed module, which exports and opens all its packages, so libraries like Spring and Hibernate can still reflect into our classes. The rules only bite when code reaches into named modules, such as the JDK's, or into our own modules if we write module-info files.
How do I find out which internal APIs my code uses?
Run jdeps --jdk-internals on the application jar and its dependencies. It lists static uses of internal packages and suggests replacements. It cannot see reflection by string name, so running the test suite on the target JDK is still the real check.
8Conclusion
In this answer, we saw that strong encapsulation adds a module boundary on top of access modifiers: only exported packages are usable, and only opened packages allow deep reflection. Java 9 enforced it gently with a permissive default and a warning, Java 16 made denial the default, and Java 17 removed --illegal-access, so the only remaining exceptions are per-package --add-opens and --add-exports flags. Critical APIs like Unsafe stayed reachable through jdk.unsupported while replacements arrived, and the whole effort exists so the JDK can evolve its internals without breaking our applications.
Open with
Strong encapsulation means that the JDK's internal packages, like sun.nio.ch or jdk.internal.misc, are no longer reachable from our code, and we can no longer use reflection to open private members of JDK classes such as String.value.
Then, if they want more
The module system made public types visible only if their package is exported, and deep reflection possible only if the package is opened. It was rolled out in stages. Java 9 enforced it but kept a permissive default for the class path, printing one warning on the first illegal access.
Java 16 changed the default to deny but still allowed --illegal-access=permit. Java 17 removed that option, so since 17 we cannot relax it globally at all.
What we can still do is open individual packages, for example --add-opens java.base/java.lang=ALL-UNNAMED, or the Add-Opens attribute in an executable jar's manifest, and I treat those as tracked exceptions. sun.misc.Unsafe kept working because it lives in jdk.unsupported, though its memory methods are now deprecated for removal.
The reason for all of it is that the JDK can only change its internals, improve performance and guarantee security if libraries stop depending on private details. In practice, the fix for most failures is upgrading the library that does the reflection.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 16, Java 17, Java 8
- 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 "What does strong encapsulation of JDK internals mean, and which JDK release removed the...": https://scrivio-interview-library.netlify.app/java/modules/cbe3657ff7/