Java requires 关键字
定义和用法
requires
关键字是一个模块指令,用于指定当前模块所依赖的另一个模块。
requires
指令可以添加两个修饰符:transitive
和 static
。
transitive
修饰符允许其他模块在使用当前模块时,无需额外声明对同一依赖模块的需求。
static
修饰符使得该依赖在运行时成为可选的。即使所依赖的模块不存在,当前模块也能正常运行。
注意:requires
关键字是一个模块指令,应在模块的 module-info.java 文件中使用。
requires
关键字是 Java 9 中引入的新特性。
实例
在模块的 module-info.java 文件中使用 requires
:
module mymodule { requires module1; requires transitive module2; requires static module3; }