What Are the Benefits of Java Module With Example
Discover the advantages of using Java modules and how they are implemented with examples. Get a clear view of this key component in modern Java development.
Join the DZone community and get the full member experience.
Join For FreeThe Java 9 release in 2017 saw the introduction of the Java Module System. This module system was developed directly for the Java language and is not to be confused with module systems such as IntelliJ Idea or Maven.
The module system helps to provide a more secure and structured approach to writing Java code by better-organizing components, thus preventing malicious or out-of-date code from being used. In this article, we will look at what exactly the Java Module System is and how it can benefit developers.
Benefits of Using Java Module
Java modules were introduced in Java 9 as a new way to organize and package Java code. They provide several benefits, including:
- Strong encapsulation: Modules allow you to encapsulate your code and hide its implementation details from other modules. This helps to reduce the risk of coupling and improve the maintainability of your code.
- Better organization: Modules help you to organize your code into logical units, making it easier to navigate and understand. You can group related classes and packages together in a module and specify dependencies between modules.
- Improved security: Modules provide a way to control access to your code and limit the exposure of sensitive APIs. You can specify which modules are allowed to access a particular module and which packages and classes within a module are exposed to the outside world.
- Faster startup time: Modules allow the Java runtime to only load the modules that are actually needed for a particular application, reducing startup time and memory usage.
How To Define Module
- Module Name
- Module Descriptor
- Set of Packages
- Dependencies, Type of resource, etc.
Let's walk through an example of a modular sample application in Java.
Our application will have two modules: com.example.core
and com.example.app
. The core
module will contain some utility classes that the app
module will use.
Here's the module descriptor for the core
module:
module com.example.core {
exports com.example.core.utils;
}
In this module, we define that it exports the com.example.core.utils
package, which contains some utility classes.
Here's the module descriptor for the app
module:
module com.example.app {
requires com.example.core;
exports com.example.app;
}
In this module, we specify that it requires the com.example.core
module, so it can use the utility classes in that module. We also specify that it exports the com.example.app
package, which contains the main class of our application.
Now, let's take a look at the source code for our application.
In the com.example.core
module, we have a utility class:
package com.example.core.utils;
public class StringUtils {
public static boolean isEmpty(String str) {
return str == null || str.isEmpty();
}
}
In the com.example.app
module, we have a main class:
package com.example.app;
import com.example.core.utils.StringUtils;
public class MyApp {
public static void main(String[] args) {
String myString = "";
if (StringUtils.isEmpty(myString)) {
System.out.println("The string is empty");
} else {
System.out.println("The string is not empty");
}
}
}
In this main class, we use the StringUtils
class from the com.example.core
module to check if a string is empty or not.
To compile and run this application, we can use the following commands:
$ javac -d mods/com.example.core src/com.example.core/com/example/core/utils/StringUtils.java
$ javac --module-path mods -d mods/com.example.app src/com.example.app/com/example/app/MyApp.java
$ java --module-path mods -m com.example.app/com.example.app.MyApp
These commands compile the core
module and the app
module and then run the MyApp
class in the com.example.app
module.
Conclusion
Java programming allows developers to employ a modular approach, which can result in smaller, more secure code. By using this technique, the code becomes encapsulated at the package level for extra security. Although there is no requirement to use this technique, it provides developers with an additional tool to potentially write higher-quality code.
Opinions expressed by DZone contributors are their own.
Comments