Quick revision
The short answer
Each --add-opens flag weakens encapsulation for one package and can break on any JDK upgrade, so each one needs an owner and a reason. We catalogue every flag in the fleet and map it to the library that needs it, since many disappear with a library upgrade. The policy then allows flags only in one central place, the platform base image or starter, never copied into individual services; blocks new flags in review unless no supported alternative exists; and re-tests on every new JDK release with the flags removed to measure progress.
What to remember
In this answer, we saw that each --add-opens flag weakens encapsulation for one package and can break on JDK upgrades, so each needs an owner and a reason. We catalogued every flag across the fleet and mapped it to the library that needs it, removed the many that upgrades or nothing at all made unnecessary, allowed the rest only in a central base image or starter, blocked new flags in review unless no supported alternative exists, and re-tested on each new JDK release with the flags removed to measure progress.
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.
Each --add-opens flag weakens encapsulation for one package and can break on any JDK upgrade, so each one needs an owner and a reason. We catalogue every flag in the fleet and map it to the library that needs it, since many disappear with a library upgrade. The policy then allows flags only in one central place, the platform base image or starter, never copied into individual services; blocks new flags in review unless no supported alternative exists; and re-tests on every new JDK release with the flags removed to measure progress.
Why this question is asked
Launch-script flags are the kind of technical debt nobody sees. They were added in a hurry during a Java 17 migration, copied from one service to the next, and now every service carries a dozen of them without anyone knowing which are still needed. They matter because each one switches off a protection the JDK added on purpose, and because the JDK keeps changing the internals they expose. Interviewers ask this at senior level to see whether we can turn a messy situation into a clear policy that teams will actually follow. In this answer, we will recap what a flag really grants, build the catalogue, decide which flags are acceptable, set the policy, and measure progress on each JDK release.
1A Quick Recap: What One Flag Grants
Since JDK 17, the JDK's own modules do not allow deep reflection into their packages: setAccessible(true) on a private field of, say, java.util.ArrayList throws InaccessibleObjectException. A flag such as --add-opens java.base/java.util=ALL-UNNAMED reverses that for one package. ALL-UNNAMED means every jar on the class path, so the grant goes not only to the library that needed it but to every library in the service, now and in the future.
Two risks follow. Security and integrity: any code can now read and change private state inside that package, which defeats the guarantees the JDK relies on. And fragility: the private members that some library reaches for are implementation details the JDK can rename or remove in any release, so a flag that works today is a promise that may silently stop being kept. Neither risk is a reason to forbid every flag, but both are reasons to know exactly why each one exists.
2Building the Catalogue
We start by finding every flag. A search over all repositories and deployment configuration, Dockerfiles, Helm charts, start scripts, JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS settings, jar manifests and test JVM arguments, finds the declared ones. Because flags can also arrive through environment variables at deploy time, we also collect each running JVM's real arguments, which RuntimeMXBean.getInputArguments() reports, through an info endpoint or a startup log line.
Then, for each flag, we find which library needs it. The quickest way is empirical: run the service's tests with that one flag removed, and the resulting InaccessibleObjectException stack trace names the library. The result is a small table, one row per flag, with the library, the version that no longer needs it, and an owner.
Notice the last entry. In practice a large share of the flags turn out to be unnecessary, copied from an old template or needed by a library version the service upgraded long ago. Removing those costs nothing, and the catalogue often shrinks by half in the first pass.
3Deciding Which Flags Are Acceptable
With the catalogue in hand, each flag falls into one of a few cases, and each case has a clear decision. A flag nobody needs is removed. A flag needed by a library that has a fixed version is removed by upgrading the library, scheduled through the platform's dependency BOM so every service moves together. A flag needed by an abandoned library is accepted temporarily, with a replacement plan and a date. A flag needed by a tool with a legitimate reason and no supported alternative, such as a profiler or a test framework, is accepted permanently but scoped as narrowly as possible, for example only in test JVMs or only for a named module instead of ALL-UNNAMED when the library is modular.
| Case | Example | Decision |
|---|---|---|
| No longer needed | Tests pass with the flag removed | Remove now |
| Library has a fixed version | Older buffer library, fixed in 2.x | Upgrade through the BOM, then remove |
| Library is abandoned | Serializer reading String internals | Accept with a dated replacement plan |
| Legitimate tool, no alternative | A profiler or test-only framework | Accept, scoped to where it is needed |
4The Policy We Set
The policy has three rules. First, flags live in one central place: the platform's base image or Spring Boot starter reads the reviewed list and applies it, and services do not add their own. That way removing a flag is a single change, and nobody copies a flag they do not understand.
Second, a new flag needs an approved request. A team that hits InaccessibleObjectException first checks for a library upgrade or a supported API; only if there is none does the platform team add an entry with the library, the reason and an owner. A CI check enforces it by failing any build or deployment file that contains add-opens outside the central list.
Third, every accepted flag has an end condition, either a library version or a review date, so the list does not grow forever. We present this to teams as a service, not a restriction: the platform owns the flags so they do not have to.
- Team hits InaccessibleObjectExceptionstack trace names the library
- Is there a fixed library version?YesUpgrade insteadNoLook for a supported API
- Is there a supported alternative?YesUse itNoAdd to the central list with owner and end date
- Applied by the base imagenever copied into a service
4 steps in order. Play them, or step through them one at a time.
5Measuring Progress on Every JDK Release
The last part of the policy turns the list into a number that moves. For each new JDK release, including early-access builds months ahead, we run every service's test suite once with all the flags removed. The failures tell us exactly which flags are still needed and which libraries still reach into internals on that release; anything that passes is removed from the list for that service.
We report the count of flags and the number of services that need any flag at all, per release. A falling count shows the policy working. A rising one, or a new failure on an early-access build, is an early warning that a library will break on the next JDK, which gives us months instead of days to deal with it.
6Follow-up questions
Is --add-opens ever the right long-term answer?
Occasionally, for tools that legitimately need deep access and have no supported alternative, such as some profilers or diagnostic agents. Even then we scope it as narrowly as possible, to one package, to the tool's module instead of ALL-UNNAMED when it is modular, and to the environments where the tool runs, and we review it on each JDK release.
What about --add-exports flags?
They are handled the same way. --add-exports makes an internal package's public types accessible at compile and run time, for code that calls internal APIs such as sun.security classes. They carry the same fragility, so they go in the same catalogue with the same rules.
How do you convince teams to give up their own flags?
By making the central list cheaper than their own: the platform team owns the investigation, the upgrades and the JDK testing, and a team that needs a new flag gets it through a short request. Showing the first catalogue, where many flags turn out to be unnecessary, usually makes the case on its own.
7Conclusion
In this answer, we saw that each --add-opens flag weakens encapsulation for one package and can break on JDK upgrades, so each needs an owner and a reason. We catalogued every flag across the fleet and mapped it to the library that needs it, removed the many that upgrades or nothing at all made unnecessary, allowed the rest only in a central base image or starter, blocked new flags in review unless no supported alternative exists, and re-tested on each new JDK release with the flags removed to measure progress.
Open with
I'd start from the fact that every add-opens flag switches off strong encapsulation for one package, and with ALL-UNNAMED it does so for every jar on the class path, including future ones.
Then, if they want more
And it exposes internals the JDK can change in any release. So each flag needs an owner and a reason. Step one is a catalogue: search all repos and deployment configs, and collect the real JVM arguments from running services, since flags also come in through environment variables.
Then for each flag, run the tests without it; the InaccessibleObjectException stack trace names the library that needs it. In my experience a lot of flags turn out to be unnecessary, copied from templates or needed by library versions long gone, and those just get removed. Others go away with a library upgrade, which I'd schedule through the platform BOM.
Abandoned libraries get a dated replacement plan. Legitimate tools without alternatives stay, scoped narrowly. The policy: flags live only in one central place, the base image or starter, never copied per service; a CI check fails any add-opens outside that list; new flags need a request showing there's no supported alternative; and each accepted flag has an end condition.
Finally, on every new JDK, including early-access builds, we run all tests with the flags removed and report the count, so progress is visible and breakage shows up months early.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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 "Your services carry many --add-opens flags in their launch scripts. How do you decide...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/f7a8299f56/