Java Modules
A module is a named set of packages, resources, and native libraries. Java 9 introduced the concept of a module, a new java entity that is similar to an executable jar in its production state, but includes 1) a name and 2) descriptor class that provides benefits that did not exist prior to Java 9.
The benefits of a module are :
1. Stronger in capsulation at a higher level which improves security
Prior to Java 9
Prior to Java 9 any class in any package with a public or protected access modifier that was included in a jar on the classpath was by its nature
publicly accessible or extensible by all of the application code. There was no way to hide this information.
In JDK 9, you have three ways to expose your public types you can make them.
- one public to everyone unqualified exposure
- public two specific modules deemed friends (qualified exposure)
- Not public to any other module but only two the package is included in the module itself
A module is a named set of packages, resources, and native libraries. Java 9 introduced the concept of a module,
a new java entity that is similar to an executable jar in its production state, but includes 1) a name and 2) descriptor class that provides benefits that did not exist prior to Java 9.
Hava Java Modules changes in Java SE 22?
Java Modules, introduced in Java SE 9 as part of the Java Platform Module System (JPMS) (also known as Project Jigsaw), have not undergone significant conceptual changes in Java SE 22. However, there have been incremental improvements and enhancements across Java versions since Java 9, including Java SE 22. Here's a summary of the changes and refinements related to modules:
-
Improved Tooling and Diagnostics
jlink
Updates: The jlink
tool (introduced in Java 9 for creating custom runtime images) continues to receive updates for better optimization and modularity support. Java SE 22 may include improvements in performance and ease of use when generating custom runtime images.
jdeps
Enhancements: The jdeps
(Java Dependency Analyzer) tool now provides more accurate diagnostics and better insights into module dependencies, especially with modern patterns like JPMS in cloud-native applications.
-
Incremental Module Refinements
- Standard Modules: Over time, standard modules have been refined to include updated APIs, better integration with newer Java features, and compatibility improvements for modern development practices.
- For example, modular versions of core libraries have been optimized for scalability and maintainability.
- Deprecated APIs: Some APIs in modules may have been deprecated or removed in favor of better alternatives. Module-based applications should always be updated to ensure compatibility with the latest JDK.
-
Better Integration with Modern Features
- Sealed Classes: Sealed classes (introduced in Java SE 15) integrate well with JPMS by allowing you to restrict which packages can subclass a class within a module.
- Records and Modules: Record classes work seamlessly in modular applications, enabling developers to take advantage of concise, immutable data structures.
- Pattern Matching: Pattern matching, evolving through multiple Java versions, integrates with modules to improve modularized code readability and maintainability.
-
Cloud-Native and JPMS
- With the growing adoption of modularity in microservices and containerized environments, Java SE 22 may have included minor improvements to module system behavior in distributed systems and cloud scenarios.
-
Backward Compatibility and Migration
- Modules remain backward compatible, allowing you to use them seamlessly with applications built on earlier versions of JPMS.
- Java SE 22 provides enhanced migration tools and practices for modularizing legacy applications.
Key Consideration:
While Java SE 22 has not redefined the module system introduced in Java SE 9, it has polished and extended the tools and APIs surrounding modules. The focus remains on improving performance, compatibility, and modern Java feature integration.
Java 9 Modularity
Benefits
A module descriptor will specifically declare which packages are visible to other modules executing in the same application space .
2. Reliable dependencies which improves stability.
A
module descriptor requires a module to specifically declare which modules it is dependent upon publishing its dependencies to consumers.
Understanding Modules
- Describe the modular JDK
- Declare modules and enable access between modules
- Describe how a modular project is compiled and run