Sub topic 11
Public Access and Module Exports
The public keyword does not always make a Java class available to every other part of a program. When a program uses modules, the module also controls which packages it shares.
A package is a named group of related Java classes. A module is a named group of packages with rules about which packages other modules can use. Modules help you share a small, useful set of classes while keeping other packages internal.
Two checks for access
For code in one named module to use a class in another named module, these checks must pass:
- The class and the members being used must be
public. - The class's module must export its package. An export makes the package's public classes available to other modules.
- The using module must require the class's module. A requirement tells Java that one module depends on another.
An export does not make a package-private class or method public. It only makes public classes available outside the module.
Build two modules
In this example, a library module shares a greeting with an app module. Each module has a module descriptor: a file named module-info.java that states its module rules.
You need JDK 9 or later. A JDK is the set of tools used to compile and run Java programs. Run these commands in an empty working folder using a Unix-like shell:
mkdir -p src/com.example.library/com/example/library
mkdir -p src/com.example.app/com/example/appCreate src/com.example.library/module-info.java:
module com.example.library {
exports com.example.library;
}The module and package happen to have the same name here. The exports line names the package that the library shares.
Create src/com.example.library/com/example/library/Greeting.java:
package com.example.library;
public class Greeting {
public static String message() {
return "Hello from the library!";
}
}Both the class and the message method are public. The method is static, so the app can call it without creating a Greeting object.
Now create src/com.example.app/module-info.java:
module com.example.app {
requires com.example.library;
}The requires line tells Java that the app needs the library module.
Create src/com.example.app/com/example/app/Main.java:
package com.example.app;
import com.example.library.Greeting;
public class Main {
public static void main(String[] args) {
System.out.println(Greeting.message());
}
}The import line lets the app use the short name Greeting. It does not replace the requires or exports rules.
Compile and run
Compiling changes Java source files into files that Java can run. From the working folder, compile both modules:
javac -d out --module-source-path src -m com.example.library,com.example.appThe -d out option puts the compiled files in the out folder. The module source path tells the compiler where to find the source files.
Run the app:
java --module-path out -m com.example.app/com.example.app.MainThe module path tells Java where to find the compiled modules. The part after -m names the app module and its starting class.
You should see:
Hello from the library!See what happens without an export
Remove the exports line from the library's module-info.java. Then run the compile command again.
Compilation fails because the app cannot use the library package. The class and method are still public, but the package is no longer exported to the app. Restore the line and compile again to fix the error.
The reverse matters too: an export alone is not enough. If the app removes its requires line, it cannot use the library module. Restore that line as well before continuing.
Export to selected modules
A library can share a package with a named module instead of every module. This is called a qualified export.
Replace the library's export line with:
exports com.example.library to com.example.app;Compile and run the example again. The app can still use Greeting, but a different named module cannot use that package through this export, even if it requires the library.
Use a qualified export when a package is meant for a specific partner module. Use a regular export when the package is meant for any module that requires the library.
Exercise
A public class is in a package that its module does not export. Can a different named module use that class just by adding a requires line?
Show answer
No. The library must also export the package to that module.