Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java's package-private visibility has proved too weak for large codebases, and many organizations never adopted JPMS modules. What strategy would you set for enforcing module and team boundaries in Java code over the next five years?

Package-private only works within one package, so any code split across packages must become public to collaborate, and JPMS, the fix Java offers, has real costs with frameworks, reflection and split packages. My five-year strategy would choose one mechanism as the default, build-level modules in Maven or Gradle plus architecture tests such as ArchUnit, put it in the templates and starters, measure violations over time, and revisit JPMS for new libraries where strong encapsulation or jlink images pay off.

Background to this question

In large Java codebases, boundaries between modules and between teams erode quietly: one convenient import at a time, until every change needs four teams to agree. Senior engineers are asked this question because the answer is a strategy, not a single tool. It needs an honest view of what Java enforces, what each option costs, how to roll it out across many teams, and how to know whether it is working. In this answer, we will recap why package-private is too weak, compare the options, choose a default and explain why, plan the rollout, and decide where JPMS still fits.

1Why Package-Private Is Not Enough

Package-private visibility hides a class from everything outside its package. That is a fine boundary for a handful of classes, but a team's component, such as billing, usually has dozens of classes organised into several packages: api, domain, persistence, messaging. Those packages must call each other, and the only modifier that crosses a package boundary is public. Once a class is public, every other team's code can import it too.

So in practice, a Java codebase has no compiler-enforced boundary between a component's internals and the rest of the organisation, unless we add one.

2The Options and What Each Costs

There are four realistic mechanisms. JPMS modules give real encapsulation: a module exports only its API packages, and the JVM refuses access to the rest. But adoption is expensive in typical enterprise code. Frameworks such as Spring and Hibernate use reflection, so packages must be opened to them; split packages, the same package in two jars, are forbidden; and many libraries are still only automatic modules. Most Spring Boot applications therefore run on the classpath, where module rules do not apply.

Build-level modules, separate Maven or Gradle modules with an api module and an impl module per component, give a boundary the compiler enforces: code cannot import a class from a module it does not depend on. The cost is more build files and slower builds if done carelessly.

Architecture tests, such as ArchUnit rules or Spring Modulith's verification for Spring applications, check dependencies between packages in a normal test. They are cheap, flexible and work inside one deployable. Their weakness is that they only catch violations at test time, and only if every repository runs them.

Conventions and reviews alone cost nothing and enforce nothing at scale.

Ways to enforce boundaries
MechanismEnforced byMain costFits
JPMS modulescompiler and JVMreflection, split packages, toolingnew libraries, jlink images
Build modules (api / impl)compiler via dependenciesmore build filescomponent boundaries
ArchUnit / Spring Modulithtests in CIrules to maintaininside one service or module
Conventions and reviewpeopledoes not scalenothing on its own

3Choosing One Default and Why

The most important decision is to pick one mechanism as the organisation's default, because three half-adopted mechanisms are worse than one fully adopted. My default would be build-level modules for boundaries between components and teams, plus architecture tests for the rules inside each module, such as domain must not depend on persistence.

The reasoning is cost against benefit. Build modules use tools every team already knows and work with Spring, reflection and the classpath. Architecture tests catch the finer-grained rules that build modules cannot express without exploding the number of modules. Together they cover almost all the value of JPMS for an application codebase at a fraction of the adoption cost.

4Rolling It Out Over Five Years

A strategy that only describes the end state fails. The rollout makes the default the easiest path. In the first year, the service template and the internal Spring Boot starters create the api and impl module layout and include a shared ArchUnit rule set, so every new service starts with boundaries. Existing services adopt the shared rules in a frozen mode, where ArchUnit records the current violations as a baseline and fails only on new ones.

From then on, we measure. A dashboard shows violations per repository, collected from CI, and architecture leads review the trend each quarter. The goal is not zero on day one; it is the number going down, with new violations blocked. A typical target is to halve the baseline each year.

Later years address the hardest boundaries, usually shared domain libraries that everyone imports, by splitting them into an API artifact and internal artifacts, and by publishing compatibility checks so the API can evolve safely.

Java
// Shared rule in the company test library; FreezingArchRule records today's violations
@ArchTest
static final ArchRule components_use_only_apis = FreezingArchRule.freeze(
noClasses().that().resideInAPackage("..billing..")
.should().dependOnClassesThat().resideInAPackage("..shipping.internal.."));
// first run: 37 violations stored as the baseline; later runs fail only on new ones
Step throughDecision for each component
  1. Is it a library with external consumers?
    YesConsider JPMS exports and jlinkNoUse the default
  2. Split into api and impl build modulesconsumers depend on api only
  3. Add shared ArchUnit rulesfrozen baseline for existing code
  4. Report violations from CIdashboard per repository
  5. Review the trend each quartertarget: baseline halves each year

5 steps in order. Play them, or step through them one at a time.

5Where JPMS Still Fits

JPMS is not wrong; it is expensive for some code and cheap for other code. For new libraries with few reflective dependencies, such as a client SDK, a protocol library or a utility, a module-info.java that exports only the API is a small cost and gives consumers on the module path real encapsulation. Applications that ship as custom runtime images with jlink, for example command- line tools or small containers, need modules anyway. So the strategy includes a review point, perhaps every two years, where we check whether framework support has improved enough to make JPMS the default for more kinds of code.

6Follow-up questions

How does Spring Modulith fit into this?

Spring Modulith treats the direct sub-packages of the application's main package as modules and provides a test that verifies that modules only use each other's API packages and have no cycles. For Spring Boot services it is an easy way to get architecture tests with sensible defaults, and it can also document the module structure. It fits the architecture test layer of the strategy.

Will more build modules slow builds down?

They can, if every module is rebuilt every time. With Gradle's build cache and configuration cache, or Maven's incremental builds, unchanged modules are skipped, and builds often get faster because smaller modules recompile less. We keep the module count proportional to real boundaries, not one module per package.

What if teams disable the rules to ship faster?

That is why the dashboard counts violations and disabled rules, not just failures. Disabling a rule shows up in the numbers and in review. We also make exceptions cheap and explicit, an allowlist entry with an owner and an expiry, so teams have a legitimate path instead of a hidden one.

7Conclusion

In this answer, we saw that package-private cannot protect code that spans packages, so large codebases need another boundary. We compared JPMS, build-level modules, architecture tests and conventions, chose build modules plus architecture tests as the single default because they give most of the benefit at a low adoption cost, and planned the rollout through templates, frozen baselines and a violations dashboard with a downward target. JPMS stays in the strategy for new libraries and jlink images, with a periodic review as framework support improves.

What to say in the interviewAbout 90 seconds aloud

Open with

The problem starts with package-private: it only hides code within one package, so as soon as a component spans several packages, its classes have to be public to work together, and then anyone can import them.

Then, if they want more

JPMS solves that with exports, but in a typical Spring codebase the cost is high, because of reflection, which needs opens, split packages, automatic modules, and the fact that most apps run on the classpath anyway. So my strategy would be to choose one default and make it the easy path.

The default is build-level modules, an api and an impl module per component, so the compiler stops cross-team imports, plus architecture tests with ArchUnit or Spring Modulith for rules inside a module, like domain not depending on persistence.

In year one, the service template and starters generate that layout and include a shared rule set, and existing services adopt the rules with a frozen baseline so only new violations fail.

Then I measure: a dashboard of violations per repository from CI, reviewed quarterly, with a target like halving the baseline each year, and explicit, expiring exceptions instead of disabled rules.

JPMS stays on the table for new libraries with few reflective dependencies and for anything shipped with jlink, and I would revisit it every couple of years as framework support improves.

About this answer

Last updated
12 November 2026
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 "Java's package-private visibility has proved too weak for large codebases, and many...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/957f17e157/