The exports keyword in Java is used within a module definition to make a package accessible to other modules. This is part of the Java Platform Module System (JPMS), introduced in Java 9, which provides a way to modularize Java applications and libraries.
Table of Contents
- Introduction
exportsKeyword Syntax- Understanding
exports - Examples
- Basic Export
- Export to Specific Modules
- Real-World Use Case
- Conclusion
Introduction
In Java’s module system, the exports keyword is used to specify which packages within a module are accessible to other modules. By default, all packages in a module are encapsulated and not accessible to other modules unless explicitly exported.
exports Keyword Syntax
The syntax for using the exports keyword in a module-info.java file is as follows:
module moduleName {
exports packageName;
}
Example:
module com.example.myapp {
exports com.example.myapp.api;
}
Understanding exports
Key Points:
- Package Visibility: Only the packages specified with the
exportskeyword are accessible to other modules. - Encapsulation: Packages not exported remain internal to the module, enhancing encapsulation and modularity.
- Selective Export: You can control which packages are exposed, providing better control over the module’s API.
Examples
Basic Export
A simple example demonstrating how to export a package from a module.
Example
// File: src/com.example.myapp/module-info.java
module com.example.myapp {
exports com.example.myapp.api;
}
// File: src/com.example.myapp/api/MyService.java
package com.example.myapp.api;
public interface MyService {
void performService();
}
Export to Specific Modules
You can export a package to specific modules only, restricting its visibility to those modules.
Example
// File: src/com.example.myapp/module-info.java
module com.example.myapp {
exports com.example.myapp.api to com.example.consumer;
}
// File: src/com.example.consumer/module-info.java
module com.example.consumer {
requires com.example.myapp;
}
Real-World Use Case
Modularizing a Library
In real-world applications, you often want to expose only certain parts of your library to consumers, while keeping other parts internal. The exports keyword allows you to achieve this.
Example
// Core module definition (src/com.example.core/module-info.java)
module com.example.core {
exports com.example.core.api;
exports com.example.core.internal to com.example.extensions;
}
// Public API (src/com.example.core/api/PublicService.java)
package com.example.core.api;
public interface PublicService {
void publicMethod();
}
// Internal API (src/com.example.core/internal/InternalService.java)
package com.example.core.internal;
public interface InternalService {
void internalMethod();
}
Consumer Module
A consumer module that uses the core module’s public API but does not have access to its internal API.
// Consumer module definition (src/com.example.consumer/module-info.java)
module com.example.consumer {
requires com.example.core;
}
// Consumer usage (src/com.example.consumer/Consumer.java)
package com.example.consumer;
import com.example.core.api.PublicService;
public class Consumer {
public static void main(String[] args) {
PublicService service = new PublicServiceImplementation();
service.publicMethod();
}
}
Extensions Module
An extensions module that has access to both the public and internal APIs of the core module.
// Extensions module definition (src/com.example.extensions/module-info.java)
module com.example.extensions {
requires com.example.core;
}
// Extensions usage (src/com.example.extensions/Extension.java)
package com.example.extensions;
import com.example.core.api.PublicService;
import com.example.core.internal.InternalService;
public class Extension {
public static void main(String[] args) {
PublicService publicService = new PublicServiceImplementation();
InternalService internalService = new InternalServiceImplementation();
publicService.publicMethod();
internalService.internalMethod();
}
}
Conclusion
The exports keyword in Java’s module system is used for managing the visibility and accessibility of packages within a module. By using exports, you can control which parts of your module are exposed to other modules, enhancing encapsulation and modularity. Understanding and effectively using the exports keyword is crucial for building maintainable and scalable modular Java applications.