Introduction to Java 9 Modules
It is also known as Project Jigsaw. In this Java 9 modules example , we will learn about modules in general and how your programming style will change in future when you will start writing modular code.
In any programming language, modules are package-like artifacts containing code, with metadata describing the module and also its relation to other modules.
Ideally, these artifacts are recognizable from compile-time all the way through run-time. Any application generally is combination of multiple modules which work together to perform the business objectives.
In terms of application architecture, a module shall represent a specific business capability.
It should be self-sufficient for that capability, and should expose only interfaces to use the module functionality. To complete its tasks, it may be dependent on other modules, which it should declare explicitly. Encapsulation means to hide implementation details, which are are not essential to know to use the module correctly. The purpose is that encapsulated code may change freely without affecting users of the module.
You can leave your ad blocker on and still support us
Stable Abstraction Abstraction helps to expose module functionality using interfaces i. Anytime, you want to change the business logic or implementation inside module code, changes will be transparent to the module users.
Explicit dependencies Modules can be dependent on other modules as well. These external dependencies must be part of the module definition itself.
These dependencies between modules are often represented as graphs.
#0 Java Tutorial - How to use Eclipse
It has been working great so far. Java has strong support for encapsulation and abstraction.
Eclipse Templates for Faster Java Development
But, explicit dependencies is where things start to fall apart. In fact, java runtime dependency resolution is so much problematic area that special tools have been created to fight this problem e.
Also, few frameworks started bundling their complete runtime dependencies as well e. Spring boot projects. With new Java 9 modules , we will have better capability to write well-structured applications.
Eclipse and Java
This enhancement is divided into two area:. I have added two modules helloworld and test. So far, modules are independent. Now suppose, we want to use HelloWorldApp.
What is a Module in General
In above code, requires keyword indicates a dependency and exports keyword identifies the packages which are available to be exported to other modules.
Only when a package is explicitly exported, it can be accessed from other modules. Packages inside a module that are not exported, are inaccessible from other modules by default. Modular applications have many advantages, which you appreciate even more, when you come across applications having non-modular codebase.
Modularity is not a silver bullet, but it is an architectural principle that can prevent these problems to a high degree when applied correctly. Whether it is right or wrong decision, only time will tell.
What is Java
It will be interesting to see, how 3rd party libraries and frameworks adapt and use the module system. And how it will impact the development work, we do everyday.
A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.
Java, J2EE & SOA Certification Training
Nice to see that you have started to come up with the tutorials on Java 9. The tutorial is good as a starter.
I have not downloaded Netbeans for JDK9 support will give it a try soon. I was just going through the example to get the concept. I would suggest you to elaborate a bit more as how to export Module and import packages.