Site iconJava PDF Blog

Java 9 Modularity explained in 5 minutes

Here at IDRsolutions we are very excited about Java 9 and have written a series of articles explaining some of the main features.

Project Jigsaw introduces modularity into Java when Java 9 is released. It is one of the biggest features of Java 9 as it was first scheduled to be released in 2011 with Java 7 and we saw ‘some baby steps’ towards it in Java 8.

Modules have taken so long because it makes fundamental changes to the way Java has worked since 1.0, and allows Java to move forward. It also allows Java to maintain compatibility while getting rid of legacy baggage (anyone still using RMI and Corba?). Getting this all done in a way which is simple, robust, backwardly compatible and rewriting large parts of Java has been a massive under-taking.

What is a module in java?
A module is collection of code. It is a holder for classes, data and other modules in the form of dependencies. It is similar to JAR files but better. Every module contains a module-info.java file which lets us explicitly set the necessary information.  including what other modules it depends on and what can be accessible by other modules.

It is a new and better way to share code for programmers and collaborate without issues.

How modules work?
A module contains a module-info.java file which contains the settings for the module including what other modules it depends on and what can be accessible by other modules.

The 2 main keywords are ‘requires’ and ‘exports’.

module Decoder{

requires Reader;
exports com.decoder;

}

The example above states that the Decoder module depends on the Reader module. The next line also exposes the com.decoder package which means that other modules can only access the com.decoder package and everything within, including classes and packages.

module Reader{

exports org.reader;

}

The example above states that the Reader module does not depend on any other module while other modules can only access the org.reader package within that module. Any other packages are not exported.

Below are 2 classes in the Reader and Decoder module respectively. The Decoder module requires the Reader module to retrieve the data to be decoded through the use of a scanner.

    package org.reader;
    public class Scanner{
        public static String name() {
            return "scanner";
        }
    }
    package com.decoder;
    import org.reader.Scanner;
    public class Main {
        public static void main(String[] args) {
            System.out.format("Reading with a %s!%n", Scanner.name());
        }
    }

Why are modules useful?
Modules bring more security with them. We can keep our code safe inside the modules which would shield the non-exported classes. No one would be able to access code which we don’t want others to see. You can now have public classes so that your code can access them without allowing other users to use them.

Modules also allow you to much more clearly get away from class path issues such as those obscure bugs which turn out to be an old version of the jar already on your class path. Java finally gets the level of dependency control we now take for granted in Maven and other tools. It is time to say good bye to class path hell.

Modules also allow us to get rid of things. So Java can keep ‘legacy’ items for users who still need them, but remove them from general use. It is time to say goodbye to a monolithic and ever-growing Java Platform.

A modular application would enable easier testing. Instead of testing the whole project, we would be able to test each module and make sure that the module is working as expected.

Modules also bring better maintainability as code could be maintained in modules. Changing code in one module won’t affect a class in a different module as long as the module still performs as it should. It is also possible to just rebuild modules instead of the whole project.
As you can tell, we are really excited by the modules support in Java 9. We are really looking forward to the change to properly lock down and modularise our large codebase. What will you be using it for?