- Learn Java 9 Modules in 15 Minutes
- Little background on Modules
- Why we need Module in Java?
- What is Java 9 Module?
- Java 9 Module – Create and use modules in Eclipse IDE
- 1. Creating a Java Project
- 2. Create module-info.java file
- 3. Create a package and a class
- 4. Export the package that we have created
- 5. Lets create another module
- 6. Lets create a class in second module
- 7. Final Step
- Top Related Articles:
- About the Author
- Comments
Learn Java 9 Modules in 15 Minutes
In this article we will learn the most important feature of Java 9 – “Java 9 Modules“. We will cover everything like why we need modules, what is a module, how to create and use Modules in Java. Lets get started.
Little background on Modules
Java Module System is a long overdue feature of Java. Module system is a part of Project Jigsaw, which was initially planned to release in Java SE 7 but got delayed and postponed to release in Java SE 8, yet again because it was a huge and prestigious project, it had been deferred from the Java SE 8 release and finally released in Java SE 9.
In the following section I’m explaining from basics so that everyone can understand the need of modules. You may find it a huge wall of text but I assure you that if you read it carefully, you will be able to understand the reason and idea behind Modules.
Why we need Module in Java?
The most important feature of java is reusability, it allows us to reuse the classes that we have created with the help of inheritance and interfaces. We can inherit behaviour of a class with the help of inheritance and can inherit the abstraction using interfaces.
To reuse these classes efficiently java has grouped them in packages and it is done in such a way so that similar type of classes are in a single package. For e.g. when we are working on collections, most of the classes and interfaces that we need are in java.util package.
Over time as our code size increased, the packages in java also got increased. Imagine working on a very large program using hundreds of packages, in such case it is very difficult to understand which classes is using what. Packages are great way of organizing classes but there needs to be way to organize packages when we need to use several of them in our code.
Also, the only way to make a class reusable between packages is to make it public and when we make it public it can be used by anyone. This needed to be addressed as well.
Java 9 introduced a cool new feature to solve the issues that we discussed above. It introduced a new feature called “Module”. “A module is a set of packages”. A module not only organizes packages, it also takes care of the accessibility so that the part of the module that we want to be reused, can be used and the part that we don’t want to be reused, cant be reused.
Till now we discussed everything theoretically, lets discuss modules with the help of codes and diagrams.
What is Java 9 Module?
A module is a set of packages. We have two types of packages in a module – 1) Exported packages 2) Concealed packages.
Exported Packages: These packages are intended to be used outside of the module, which means any program residing in any other module can use these packages.
Concealed Packages: These packages are not intended to be used outside, they are internal to the module and can be used inside the module only.
To further understand the concept of exported packages and concealed packages, lets take an example of “java.base” module.
Lets understand this with the help of following diagram. In the following diagram, the green block in java.base represents “exported packages”, there are several number of exported packages but I have mentioned only few of them. These packages mentioned in the green block can be used by outside class.
The packages in the yellow block are concealed packages, they are not accessible outside the module. These packages are meant to be used only inside the module.
In java we define the module in module-info.java file and to mention any package as exported packages we mention the name of package after exports keyword as shown in the right side(grey) block in the diagram below.
Lets write programs in Eclipse IDE and use the concept of Module.
Java 9 Module – Create and use modules in Eclipse IDE
We will create a class in one module and use that class in another module.
1. Creating a Java Project
We are creating a java project in Eclipse IDE. The project name we are using is “beginnersbook.demo”.
2. Create module-info.java file
Once the project is created, right click on the project name, go to the “Configure” option and click “create module-info.java” option as shown in the following screenshot. Give the module name same as project name “beginnersbook.demo“.
For now leave the file empty. We will come to it later.
3. Create a package and a class
We are creating a class in this module and we will use this class in another module. We are creating a class “BeginnersBook” in the package “beginnersbook.demo“.
Source code of the class BeginnersBook:
package beginnersbook.demo; public class BeginnersBook < public String welcomeMessage() < return "Welcome to BeginnersBook"; >>
4. Export the package that we have created
Since we are planning to use the class “BeginnersBook” in another module, lets make this package exported so that it can be used outside of the module. To do this, write this code in module-info.java file –
Final Structure of project beginnersbook.demo looks like this:
5. Lets create another module
Lets create a new java project, name it as “beginnersbook.user” and create the module-info.java file the same way that we have created above and name it as “beginnersbook.user”. The module-info.java file of this project has this code:
//module-info.java file module beginnersbook.user
Since we are planning to use the beginnersbook.demo package, we mention the package name after requires keyword as shown in the above code.
Note: You will most likely get an error message saying that the package name is not resolved, this is because the other module is not in the build path. To resolve this error, right click on the project beginnersbook.user, go to Build Path -> Configure Build Path as shown in the following screenshot:
Go to Projects tab, click on Modulepath then click on “Add” button on the right side and add the previous java project “beginnersbook.demo” that we have created. Your final screen should look like this:
Your error should be resolved now.
6. Lets create a class in second module
Create a class “BeginnersBookUser” in this project under package “beginnersbook.user“.
Source code of the class BeginnersBookUser:
package beginnersbook.user; import beginnersbook.demo.BeginnersBook; public class BeginnersBookUser < public static void main (String arg[]) < BeginnersBook obj = new BeginnersBook(); System.out.println(obj.welcomeMessage()); >>
Final Structure of both projects looks like this:
7. Final Step
Lets run the BeginnersBookUser class, you should get the following output:
Thats it guys. To give a brief, we have created a class in a module and exported the package so that the class can be used outside the module.
To test it, we have created another class in a different module and used the exported package and finally used that class that we have created first.
Top Related Articles:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Comments
I must say its a great starting point to understand java 9 modules. Spending 20 minutes and got understanding of JAVA 9 modules. Keep it up.