Java MCQ: How does the module-info.java file contribute to Java 9’s module system?
Answer:
Explanation:
The module-info.java
file is a key component of Java 9’s module system. It specifies dependencies between modules and defines the boundaries of a module. The module-info.java
file is placed in the root directory of a module and contains declarations that describe the module’s name, the packages it exports, and the other modules it depends on.
Here’s an example of a module-info.java
file:
module com.example.myapp {
requires java.sql;
exports com.example.myapp.services;
}
In this example, the module com.example.myapp
requires the java.sql
module and exports the package com.example.myapp.services
to other modules. This setup enforces strong encapsulation, ensuring that only the specified packages are accessible outside the module, and it clearly defines the module’s dependencies.
The module-info.java
file plays a crucial role in the modularization of Java applications, enabling better organization, maintenance, and deployment of code.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html