Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

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

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

For example:

module bar {
    requires java.compiler;
    requires static java.base;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...