requires and exports
Control dependencies and visibility.
requires and exports is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Controlling the Module Graph
Two directives shape the module graph: requires declares what you depend on, and exports declares what you reveal.
This lesson explores their variants and the readability/accessibility rules they create.
Plain requires
A simple requires X; means "this module reads module X." Your code can use X's exported public types, but consumers of your module learn nothing about X.
module com.example.app {
requires java.sql;
}requires transitive
requires transitive Y; means any module that reads you also reads Y. This is implied readability, useful when your API exposes Y's types.
module com.example.api {
requires transitive java.sql;
}When to Use transitive
Use transitive when a method in your exported API returns or accepts a type from another module. Otherwise callers could not even name the return type without their own requires.
It saves consumers from declaring redundant dependencies.
requires static
requires static Z; declares a compile-time-only dependency. The module is needed to compile but is optional at run time, perfect for annotation processors or optional integrations.
module com.example.app {
requires static com.example.annotations;
}Plain exports
exports pkg; makes the public types of pkg readable by all modules that require yours.
module com.example.lib {
exports com.example.lib.api;
}Qualified exports
A qualified export reveals a package to only specific named modules using exports pkg to modA, modB;. Useful for friend modules within a larger system.
module com.example.core {
exports com.example.core.internal to com.example.web, com.example.batch;
}opens for Reflection
exports grants compile-and-runtime access to public members. Deep reflection (private fields) requires opens pkg; instead, used by frameworks like Jackson or Hibernate.
module com.example.model {
exports com.example.model;
opens com.example.model.entities to com.fasterxml.jackson.databind;
}open Modules
If an entire module needs reflective access (common for legacy frameworks), declare it open. Every package is then open for reflection, though normal exports still control compile-time access.
open module com.example.beans {
requires java.logging;
}Readability vs Accessibility
Two conditions must both hold to use another module's type:
- Readability: your module
requirestheirs - Accessibility: their module
exportsthe package and the type is public
Miss either and the compiler rejects your code.
Putting It Together
A well-formed module that re-exposes JDBC types and opens entities for a JSON library:
module com.example.service {
requires transitive java.sql;
requires static com.example.annotations;
exports com.example.service.api;
exports com.example.service.spi to com.example.plugins;
opens com.example.service.dto to com.fasterxml.jackson.databind;
}Quick Check
Pick the directive for re-exposed API types.
Recap
You learned the dependency and visibility directives:
requires,requires transitive,requires staticexportsand qualifiedexports ... toopensandopen modulefor reflection- Use needs both readability and accessibility
Next: decoupling with services via provides and uses.
Frequently asked questions
Is the “requires and exports” lesson free?
Yes — the full text of “requires and exports” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “requires and exports”?
Control dependencies and visibility. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “requires and exports” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- module-info.java
- requires and exports
- Services with provides/uses
- Migrating to Modules