Posted on: 28 May 2019

Share this:

What is modularity and how can you implement that with Java 9?

file

Modularity in programming is a form of writing code and implementing a program in the form of unique modules. It is quite different from monolithic design and helps reduce a system’s complexity and a few other features. Java 9 has implemented modularity for many years and is a great example of this type of architecture.

What are the advantages of Modularity?

Before we dive into a few more details, let’s understand why we need a modular approach to coding:

1. Efficiency is an important reason. We want to avoid small devices running the full JDK unnecessarily. Java 9 has achieved this via the Jlink tool.
2. Even on large devices, space is still an important resource, so using it efficiently is a high priority.
3. In object-oriented programming, removing flaws in encapsulation is a necessity. In Java 9, Public is no longer visible unless exported to module-info.java. It checks dependencies during both compile time and runtime. So even with reflection, we cannot access it, when it is not exported.
4. Sometimes developers prefer to hide their implementation and give it visibility only via an interface.
5. To improve security and maintainability and prevent internal APIs from using it.
6. To improve performance.

What is a Java 9 Module?

A module is a module-info.java file also known as a module descriptor. Its function is to describe our module and hence it is called the module descriptor. The name of a module should always be unique as it defines two things:

1. Which other modules our module depends on.
2. Which packages are to be exported to be used by other modules.

Types of Modules

Below are the different types of modules along with their descriptions:

1. Application modules:Modules created to achieve functionality and all third-party dependencies fall in this category.
2. Automated modules: JARs files that do not have module descriptors but are placed in the module path become automated modules. They export all packages and read all other modules. They even rename pre-Java 9 JARs like this:
Eg: mysql-connector-java-5.1.5.jar to mysql.connector.java
3. Unnamed modules: All JARs and classes on the class path become unnamed modules. They do not have any names and so it can read and export to all modules.
4. Platform modules: The Java Development Kit itself has been migrated into a modular structure. Those modules are called platform modules, eg: java.se, java.xml.ws

Types of Module Descriptors

Since Java modules have module descriptors, here’s a quick explanation of the popular ones below. By default, every module can read the java.base module. It is recommended that module names use a reverse-domain pattern.

1.requires <module>
The requires command in the module descriptor is used to define modules in which this particular module is dependent on.

2.requires transitive <module>
The transitive command exports everything to a module that asks this specific module.

3.requires static <module>
The static keyword is used when the dependency check is mandatory at compile time and optional at runtime.

4.exports <package-name>
This module exports the named package to other modules. If the package is not exported, other modules cannot use it.

5.exports <package-name> to <module-name>
Also known as a qualified export, this command exports the package to a specifically named module and not all the modules.

6.opens <package-name>
The opens keyword is used to access a particular package through Reflection in runtime, or to access the public type at compile time, and the private type at the runtime.

7.opens <package> to <module>
Also known as a qualified open, it is used to access a specific package through Reflection in runtime by only a specified module.

8.Uses and Provides:

a.Uses <service-interface>: This specific interface is in this module and implementation can be in this module or some other module.

b.Provides <service-interface> with <class1>,<class2>: The implementation is mentioned for the interface.

In part 2 of this topic, we will discuss how a Package & Project Structure would look like, and we’ll shed some light on the Jlink tool and how to create jmod files from Modules. We hope you’ve learned something useful from this article.

Do subscribe to our newsletter for more and do contact us for all your Java requirements. Asahi Technologies is one of the top custom software development companies in the world, with a team of developers with more than 10 years of experience.

Jayaramanan

Jayaramanan

Software Engineer

Recommended further reading:

0Comments