Java Module Benefits With Example
This article will quickly see what the Java module system does and how it benefits writing more secure and structured code.
Join the DZone community and get the full member experience.
Join For FreeJava SE 9 introduced the Java module systems. It should not be confused with modules from maven or IntelliJ Idea etc. Java SE 9 module is a new feature directly supporting Java.
In this article, we will quickly introduce what the Java Module system does and how it benefits writing more secure and structured code.
Benefits of Using Java Module
Some of the benefits of using modular Java code are as follows:
Strong Encapsulation: No Unintentional Classes in Global Space
A Java module must explicitly tell which Java packages are to be exported (available) to other Java modules. This makes strong encapsulation at the module level.
Reduce Compile-Time Dependencies
When we have dependencies on other packages, A Java module must explicitly tell which modules are needed. That saves time since, with modules, JVM can quickly locate dependencies and load them.
Smaller Size of Deployable Resources
Since we know all the dependencies that our application needs, we can ship only the required packages, which results in the smaller size of the application.
Improve Deploy Reliability
With dependencies specified through module definition, all the dependent packages are already known starting from development time. This means we will not have issues after deployment where runtime exception occurs due to missing dependencies.
How to Define Module
A module is described using a file with the name module-info.java. Module Definition contains the following information:
- Module Name
- Module Descriptor
- Set of Packages
- Dependencies, Type of resource, etc.
In the following section, we will create a Java Modular application with two modules, which would help and also provide a good start to explore Java Modules.
Our Modular Sample Application
We will write a small application containing two modules.
Module 1 (car.shop): The first module is a Car Shop module which provides various functionalities like fixing a car, changing tires, etc. It depends on DTO/Model classes provided by module 2. Following is the definition of a car.shop module.
module car.shop {
exports car.shop;
requires car.common;
}
Module 2 (car.common): Provides various DTO classes; the following is the definition of this module.
Here, in this module, we are exporting one package with name car.common.model. That means even though this particular module might have many packages, we are making sure the packages are encapsulated. Only packages that we feel should be available to the outside world should be exported.
module car.common {
exports car.common.model;
}
Sample Code
Let's see our sample service class, which is defined in Module 1.
package car.shop;
import car.common.model.Car;
public class CarService {
public static void main(String[] args) {
Car car = new Car();
changeTire(car);
}
public static void changeTire(car.common.model.Car car) {
System.out.println("Car Service");
}
}
The above code looks very familiar nothing special about it. We are defining a static method changeTire which takes a parameter of type car.common.model.Car.
Now this particular package car.common.model is defined in Module 2, and since we defined both the modules, the system knows at compile time that we have a dependency and how to resolve it.
So far, it looks good, but then what is the advantage?
The advantage is that we are dictating the relationship between packages at the module level. Now imagine a car.common contains a lot of other packages like car.internal, car.workin.progress, car.private.package, etc; without Modular Java, it was not possible to pick only the required packages in the application, and we would have to bundle all packages together whether we need or not.
With Java Modules, not only can we encapsulate the packages that need to be available to the outside world (export car.common), but we are also making sure Classes or Packages that should not be exposed are hidden from the outside world.
The full source code of the Java Module project can be found on GitHub.
Summary
It is not mandatory to utilize the Java program in a modular fashion. Still, it definitely provides a new tool in the hands of developers to utilize this new concept to write more efficient (small size) and secured (encapsulation at package level) code.
Opinions expressed by DZone contributors are their own.
Comments