What's the difference between requires and requires static in module declaration

为君一笑 提交于 2019-11-28 09:46:41

A requires clause expresses that the required module is needed at compile and run time. Consequently, when the module system encounters such a clause during module resolution (the phase in which module descriptors are processed and dependencies are resolved) it searches the universe of observable modules (the modules in the JDK and on the module path) and throws an error if it doesn't find the module.

A requires static clause expresses a dependency that is optional at run time. That means at compile time the module system behaves exactly as described above.

At run time, on the other hand, it mostly ignores requires static clauses. If it encounters one, it does not resolve it. That means, if an observable module is only referenced with requires static, it does not make it into the module graph! This can be a little surprising at first. If, on the other hand, the module makes it into the graph in some other way (required by some other module, added manually with --add-modules, drawn in by service binding), all modules that have an optional dependency on it can read it.

The primary difference between the two is that in case of

requires static foo.module;

The dependence is mandatory in the static phase, during compilation, but is optional in the dynamic phase, during execution while on the other hand

requires bar.module;

Is added to declare that the module depends, by name, upon some other modules, at both compile time and run time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!