
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the benefits of a module in Java 9?
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.
- Related Articles
- What are the characteristics of a module in Java 9?
- What are the different compilation modes of a module in Java 9?
- What are the different module types in Java 9?
- What are the benefits of immutable collections in Java 9?
- What are the components in a module-info file in Java 9?
- What are the advantages and disadvantages of the Module System in Java 9?
- Importance of Module Descriptor in a Module in Java 9?
- What is Module System in Java 9?
- What is an unnamed module in Java 9?
- What is the use of the "requires" clause in a module-info file in Java 9?
- What is the use of the "export" clause in a module-info file in Java 9?
- How to create a module in Java 9?
- When to use the ServiceLoader class in a module in Java 9?
- What are the advantages of Modules in Java 9?
- How can we display all module names in Java 9?

Advertisements