STUDYECRAT Java 9 Module System
60s
Java9 Module System
Prove your skills in this interactive quiz
Live Code
Run snippets directly
Timed
60s per question
Scored
Earn 3D badges
★ Java 9 Module System: Key Interview Points
1. Module Declaration Syntax
module com.example.app {
requires java.sql;
exports com.example.api;
opens com.example.internal;
}
// Requires: Dependencies | Exports: Public API | Opens: Reflection access
requires java.sql;
exports com.example.api;
opens com.example.internal;
}
// Requires: Dependencies | Exports: Public API | Opens: Reflection access
- Tip: Always place module-info.java in your root source folder
- Real Use: Breaking monolithic apps into secure, encapsulated components
2. Transitive Requires
module framework {
requires transitive logging.lib;
exports com.framework.core;
}
// Clients of framework automatically get logging.lib
requires transitive logging.lib;
exports com.framework.core;
}
// Clients of framework automatically get logging.lib
- Tip: Use for mandatory dependencies that clients will need
- Real Use: Framework libraries exposing their dependency contracts
3. Service Provider Modules
// Provider module
module db.driver {
provides com.db.Driver with com.db.MyDriver;
}
// Consumer module
module app {
requires com.db;
uses com.db.Driver;
}
// Loose coupling between service interfaces and implementations
module db.driver {
provides com.db.Driver with com.db.MyDriver;
}
// Consumer module
module app {
requires com.db;
uses com.db.Driver;
}
// Loose coupling between service interfaces and implementations
- Tip: ServiceLoader automatically discovers all providers
- Real Use: Database drivers, plugin systems, and SPI patterns
4. Restricted Package Exposure
module security {
exports com.security.api;
exports com.security.spi to com.trusted.client;
opens com.security.internal to com.testing;
}
// Fine-grained control over package visibility
exports com.security.api;
exports com.security.spi to com.trusted.client;
opens com.security.internal to com.testing;
}
// Fine-grained control over package visibility
- Tip: Use for internal APIs that should only be accessible to specific modules
- Real Use: Enterprise applications with strict security requirements
5. Automatic Modules
// Non-modular JAR on module path becomes automatic module
requires legacy.lib; // Derived from JAR filename
// Command to check module name:
jar --describe-module --file=legacy-lib.jar
// Temporary solution for migration
requires legacy.lib; // Derived from JAR filename
// Command to check module name:
jar --describe-module --file=legacy-lib.jar
// Temporary solution for migration
- Tip: Automatic modules read all other modules and export all packages
- Real Use: Gradually modularizing large codebases with third-party libs
💡 Pro Interview Tip
When asked about modules, always mention three key benefits: strong encapsulation, reliable configuration, and improved performance. Be ready to explain how modules solve JAR hell and improve security.