DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Troubleshooting, Dynamic Logging, and Observability for .Net — A New Kid on the Block
  • Building a Data Warehouse for Traditional Industry
  • [DZone Survey] What’s Your Superpower?
  • 5 Best Java Frameworks for Web Development in 2023
  1. DZone
  2. Coding
  3. Java
  4. What Are the Benefits of Java Module With Example

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.

Janki Mehta user avatar by
Janki Mehta
·
Mar. 23, 23 · Tutorial
Like (2)
Save
Tweet
Share
4.96K Views

Join the DZone community and get the full member experience.

Join For Free

The 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:

Java
 
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:

Java
 
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:

Java
 
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:

Java
 
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:

Java
 
$ 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.

Java language application Java (programming language) security systems

Opinions expressed by DZone contributors are their own.

Trending

  • Troubleshooting, Dynamic Logging, and Observability for .Net — A New Kid on the Block
  • Building a Data Warehouse for Traditional Industry
  • [DZone Survey] What’s Your Superpower?
  • 5 Best Java Frameworks for Web Development in 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: