Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

What do requires transitive and requires static mean in module-info, and when would you use each?

requires transitive M means that any module that reads ours also reads M automatically, which is called implied readability; we use it when types from M appear in our exported API, so consumers can use our API without declaring M themselves. requires static M means M is needed at compile time but optional at run time; we use it for optional integrations and annotation-only dependencies, and the code that touches M must cope with M being absent when the program runs.

Why this question is asked

A plain requires is easy to understand: my module needs that module. The two modifiers change that promise in opposite directions. transitive passes the dependency on to our consumers, and static makes it disappear at run time. Interviewers ask this to see whether we understand readability in the module system, and whether we can tell when each modifier is right, because getting it wrong produces confusing compile errors for consumers or NoClassDefFoundError at run time. In this answer, we will recap readability, see the problem requires transitive solves, look at how the JDK itself uses it, then look at requires static, how its modules are resolved, and how code must handle their absence.

1A Quick Recap: Readability

In the module system, code in one module can only use types from another module if the first module reads the second. requires M is how we declare that our module reads M. Readability is not transitive by default: if module A requires B, and B requires C, A does not read C. A can only use types from modules it requires itself (plus java.base, which every module reads).

That default is good encapsulation: B's dependencies are B's business. But it causes a problem as soon as B's public API mentions a type from C, because then A cannot use B's API without also seeing C.

2The Problem requires transitive Solves

Suppose our module com.acme.orders.api offers a repository whose method takes a javax.sql.DataSource, a type from the java.sql module. Our module requires java.sql, so it compiles. Now an application module requires our module and calls the method.

Java
module com.acme.orders.api {
requires java.sql; // plain requires
exports com.acme.orders.api;
}
public interface OrderRepository {
List<Order> findOpen(DataSource dataSource); // javax.sql.DataSource, from module java.sql
}
module com.acme.app {
requires com.acme.orders.api;
}
// In com.acme.app:
DataSource ds = createDataSource();
// error: package javax.sql is not visible
// (package javax.sql is declared in module java.sql, but module com.acme.app does not read it)

The application is forced to add requires java.sql just to call our API, and if our API later starts using another module's types, every consumer breaks again. Our API leaked a dependency without passing on the readability it needs.

3Implied Readability With requires transitive

requires transitive fixes this by making the dependency part of our module's contract: every module that reads com.acme.orders.api now also reads java.sql, automatically.

Java
module com.acme.orders.api {
requires transitive java.sql; // readers of this module also read java.sql
exports com.acme.orders.api;
}
module com.acme.app {
requires com.acme.orders.api; // no requires java.sql needed
}
// In com.acme.app:
List<Order> open = repository.findOpen(createDataSource()); // compiles
requires, requires transitive, requires static
FormAt compile timeAt run timeUse it when
requires MWe read MM must be presentM is used inside our implementation
requires transitive MWe and our readers read MM must be presentM's types appear in our exported API
requires static MWe read MM is optionalOptional integration or annotations only
requires transitive static MWe and our readers read MM is optionalRare: optional API types

The rule of thumb is simple: if a type from module M appears in the signatures of our exported packages, as a parameter, a return type, a superclass or an exception, we require M transitively. If M is used only inside our implementation, a plain requires is right, because consumers should not depend on it. The compiler can help: javac -Xlint:exports warns when an exported API mentions a type from a module that is not required transitively. The JDK follows the same rule: the java.sql module itself declares requires transitive java.logging and java.xml, because its API uses types from both, and java.se is an aggregator module made almost entirely of requires transitive lines.

4Requires static: Needed to Compile, Optional to Run

requires static goes in the other direction. The module must be present when we compile, so our code can refer to its types, but at run time it is optional: if it is missing, the application still starts. Two situations call for it.

The first is annotations that only matter to the compiler or to tools. Nullness annotations such as JSpecify's @Nullable, or SpotBugs annotations, are read by static analysis; nothing needs them at run time. The second is optional integrations: our library offers JSON support if Jackson is present and works fine without it.

Java
module com.acme.orders {
requires static org.jspecify; // @Nullable, @NonNull: compile time only
requires static com.fasterxml.jackson.databind; // optional JSON integration
exports com.acme.orders.api;
}

But there is a catch that surprises people: a module required only statically is not resolved at run time even if its jar is on the module path. The module system adds it only if some other module requires it normally, or if we add it on the command line with --add-modules. So in a modular application, if the app itself requires Jackson, our optional integration sees it; if nobody does, it stays absent even though the jar is right there.

5Handling the Absent Module at Run Time

Code that uses a static dependency must not touch its types unless it knows the module is there. If it does, the JVM throws NoClassDefFoundError the first time a class referring to those types is loaded or linked. The safe pattern is to keep all the optional code in a separate class and only load that class after checking.

Java
public final class OrderExporter {
private static final boolean JACKSON = isPresent("com.fasterxml.jackson.databind.ObjectMapper");
public String export(Order order) {
return JACKSON ? JacksonSupport.toJson(order) // only loaded when Jackson is present
: order.toString(); // fallback without Jackson
}
private static boolean isPresent(String className) {
try {
Class.forName(className, false, OrderExporter.class.getClassLoader());
return true; // true with Jackson resolved
} catch (ClassNotFoundException | LinkageError e) {
return false; // false when the module is absent
}
}
}
// JacksonSupport is a separate class that imports ObjectMapper; it is never loaded if JACKSON is false

Notice that OrderExporter itself never mentions ObjectMapper in a field or signature; only JacksonSupport does. The class-name check works both on the module path and on the class path, and in a modular application ModuleLayer.boot().findModule("com.fasterxml.jackson.databind").isPresent() is another clear way to ask. Annotation-only dependencies need no such check: annotations whose classes are missing at run time are simply ignored when the JVM reads them.

6Follow-up questions

Can a module both require something transitively and statically?

Yes, requires transitive static M is allowed. It means our readers also read M at compile time, but M is optional at run time. It is rare, mainly for optional API types such as annotations that consumers should also see when compiling.

Is requires transitive like a Maven compile-scope dependency?

It is similar in spirit to the api configuration in Gradle's java-library plugin, which puts a dependency on consumers' compile class path, while a plain requires is like implementation. The build tool and the module descriptor should agree, otherwise compiling works in one tool and fails in the other.

What does requires static correspond to in Maven?

Usually an optional dependency, marked optional or provided scope. The jar is available when compiling the library, but consumers do not get it transitively, matching the run-time optionality in the descriptor.

7Conclusion

In this answer, we saw that readability is not transitive by default. requires transitive gives implied readability, so modules that read ours also read the required module, and we use it whenever that module's types appear in our exported API, as the JDK's java.sql does with java.logging and java.xml. requires static makes a module mandatory at compile time but optional at run time, for annotations and optional integrations; such modules are only resolved if something else requires them or they are added with --add-modules, so our code must isolate and check before using them.

What to say in the interviewAbout 80 seconds aloud

Open with

Both modify a plain requires, which says my module reads another one. Readability is not transitive by default, so if my API mentions a type from another module, my consumers would have to require that module themselves. requires transitive fixes that with implied readability: anyone who reads my module automatically reads the transitively required one.

Then, if they want more

I use it when types from that module appear in my exported API, for example a method taking a javax.sql.DataSource means requires transitive java.sql. The JDK does this itself: java.sql requires java.logging and java.xml transitively.

If the dependency is only used internally, I use a plain requires so I do not leak it. requires static is the opposite direction: the module must be there at compile time but is optional at run time. I use it for annotation-only dependencies, like nullness annotations, and for optional integrations, like JSON support if Jackson is present.

Two things to remember: a static dependency is not resolved at run time unless some other module requires it or it is added with --add-modules, and any code that uses it must handle its absence. I keep that code in a separate class and only load it after checking the module or class is present, otherwise I get NoClassDefFoundError.

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 "What do requires transitive and requires static mean in module-info, and when would you...": https://scrivio-interview-library.netlify.app/java/modules/c9a4a40a34/