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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • A Beginner's Guide to Back-End Development
  • Providing Enum Consistency Between Application and Data
  • JQueue: A Library to Implement the Outbox Pattern

Trending

  • Design Patterns for GenAI Creative Systems in Advertising
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • Leveraging Apache Flink Dashboard for Real-Time Data Processing in AWS Apache Flink Managed Service
  • Spring Boot Done Right: Lessons From a 400-Module Codebase
  1. DZone
  2. Coding
  3. Java
  4. Java Module Benefits With Example

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.

By 
Siddhartha Bhattacharjee user avatar
Siddhartha Bhattacharjee
·
Feb. 22, 23 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

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

Java
 
module car.common {
	exports car.common.model;
}


Sample Code 

Let's see our sample service class, which is defined in Module 1.

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

Java (programming language) Database Java virtual machine Library application

Opinions expressed by DZone contributors are their own.

Related

  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • A Beginner's Guide to Back-End Development
  • Providing Enum Consistency Between Application and Data
  • JQueue: A Library to Implement the Outbox Pattern

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook