266.
267.
Consider the following code snippet:
package mypackage;
public class MyClass {
public void doSomething() {
}
}
In order for this code to be deployed as part of a module,
what needs to be done?
Answer:
The code above specifies a class
called MyClass as part of a
package called mypackage. In order for this code above to be
deployed as part of a module, a module
descriptor needs to be
created as follows:
module mymodule {
exports mypackage;
}
So, this specifies a module called mymodule. It uses the exports
directive with the package mypackage indicating that
mypackage will be available to other modules.
Explain the requires static module directive. When should
it be used?
Answer:
The
requires static directive
is used to specify optional
dependencies, that is dependencies that are required at compile
time but not required at runtime. Suppose you have developed a
module and require some third–party
library which the end users
of your module will never require. In such a case, you can specify
the third–party library via the requires
static directive while
defining your module. So, the users of your module will need to
have the third–party library at compile time, but they do not need
it at runtime.