Modules
Last updated
Was this helpful?
Last updated
Was this helpful?
We used to have only accessibility modifiers on class level in Java. Module is way to encapsulate classes in our code. That means, we can control what is accessible from our library.
Modules helps us to avoid big ball of mud, aka big ball of classes loaded from all the jars. Which means, we could optimize number of classes that are loaded into memory. That should result in smaller memory foot print.
Module system is not mandatory. We can create apps or libraries without module system. But if we decide to use modules, so we create a module-info.java file, we need to say what other modules should be included.
Here is nice explanation how it is with .
First lets create a module.
Then we create module-info.java. We need to insert requires java.logging because we want to use Logger from java.utils.logging package. There can be only one module-info.java file per module and it should be located in root source folder.
java.base module is included automatically
If we want to let other modules to access our classes, we need to export those classes from our module.
When an external module needs to access our classes using reflection, we need to export it to that module.
Or we can open our code to other libraries.
can be used to analyze dependencies in our modules and build our module-info.java files.
can be used to assemble a runtime image.