Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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; } 
like image 910
Michał Szewczyk Avatar asked Oct 03 '17 04:10

Michał Szewczyk


People also ask

What does transitive modifier mean?

The " transitive " modifier means that what " required " in java.se (i.e. all standard Java SE modules) will be required to client code as soon as client module definition has " requires java.se; " Another approach to compile the code would be to require specifically the SQL module: requires java.

Is module info java required?

In order to declare a module, we need to include the "module-info. java" file to root source code. 1) requires <module>: By default, a module doesn't know other modules present in a module-path. So, it is necessary to add a line in our module-info.

What is a java modular project?

A Java module is a packaging mechanism that enables you to package a Java application or Java API as a separate Java module. A Java module is packaged as a modular JAR file. A Java module can specify which of the Java packages it contains that should be visible to other Java modules which uses this module.


2 Answers

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.

like image 124
Nicolai Parlog Avatar answered Sep 28 '22 18:09

Nicolai Parlog


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.

like image 43
Naman Avatar answered Sep 28 '22 19:09

Naman