What are the benefits of a module in Java 9?

JavaObject Oriented ProgrammingProgramming

An important feature introduced in Java 9 is Module. By using a module, we can divide the code into smaller components called modules. It means each module has its own responsibility and declare its dependency on other modules to work correctly.

Below are the steps to create a modular project in Java 9:

Initially, we can create a file named "module-info.java" and add to a package(module) for which it is created. For instance, if our package name is com.mycompany.mypackage then the file goes to the same package (src/com.mycompany.mypackage/module-info.java). We can create a module by declaring "exports" and "requires" expressions.

If our modules require another module we can write below code

module com.tutorialspoint.greetings {
   requires org.tutorix;
}


To expose module contents, we can write below code

module org.tutorix {
   exports org.tutorix;
}


Benefits of a module:

  • The modules hide unwanted and internal details very safely for better security. It means that a module can access only exported package contents and not all the contents or public/internal API of other modules, so the public in one module is not public to other modules.
  • The application becomes small and fast because we can use only whatever modules we want.
  • Easy deployment on small devices as memory requirements are very less.
  • Easy to support the single responsibility principle.
  • Easy to support less coupling between components.
raja
Published on 26-Feb-2020 10:04:14
Advertisements