26

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;
}
Naman
  • 27,789
  • 26
  • 218
  • 353
Michał Szewczyk
  • 7,540
  • 8
  • 35
  • 47
  • 3
    Possible duplicate of [Does the Java 9 Module system support optional dependencies?](https://stackoverflow.com/questions/39900460/does-the-java-9-module-system-support-optional-dependencies) – ZhekaKozlov Oct 03 '17 at 04:48
  • 1
    @ZhekaKozlov Though I support the duplicate vote. But not very sure if the linked post state the *difference* between the two. Anyway, I also do believe that the primary motive behind this question could be to know about `static` modifier on the `requires` directive itself. – Naman Oct 03 '17 at 04:58
  • 3
    I don't consider this to be a duplicate. While both questions deal with the same topic, they have very different angles. This question here will be found by people searching for the syntax, the other one by people having the requirement to make a dependency optional. – Nicolai Parlog Oct 03 '17 at 16:09

2 Answers2

28

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.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • 1
    Could you provide use case when I need requires static ? Why should I want to have some dependency only during compile time? – gstackoverflow May 13 '19 at 12:42
  • 1
    I think that goes beyond the scope of this question, but I give two examples [in my blog post about optional dependencies](https://blog.codefx.org/java/module-system-optional-dependencies/#The-Conundrum-Of-Unrequired-Dependencies). – Nicolai Parlog May 15 '19 at 08:18
7

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Could you provide use case when I need requires static ? Why should I want to have some dependency only during compile time? – gstackoverflow May 13 '19 at 12:42
  • 5
    @gstackoverflow lombok and other annotation processing tools (e.g. non-runtime annotations) – Etki May 18 '19 at 09:25
  • @Etki post this as an answer, it took me 5 minutes to read both answers, to only "get it" via your comment. And this happened the third time in 3 years now! please... – Eugene Jul 27 '20 at 02:35