I started to learn jigsaw java-9 feature and read some articles/video.
I can't understand concept of optional dependencies(requires static
)
quote from article:
When a module needs to be compiled against types from another module but does not want to depend on it at run time, it can use a requires static clause. If foo requires static bar, the module system behaves different at compile and run time:
At compile time, bar must be present or there will be an error. During compilation bar is readable by foo.
At run time, bar might be absent and that will cause neither error nor warning. If it is present, it is readable by foo.
So I want to know couple of things:
What the reason to make module dependable on another module during compile time but not in runtime? any examples? instruments like lombok?
Any analogs of optional dependencies in java prior java-9 ?
P.S.
I found one more explanation: quote from article:
Sometimes we write code that references another module, but that users of our library will never want to use.
For instance, we might write a utility function that pretty-prints our internal state when another logging module is present. But, not every consumer of our library will want this functionality, and they don’t want to include an extra logging library.
In these cases, we want to use an optional dependency. By using the requires static directive, we create a compile-time-only dependency:
module my.module {
requires static module.name;
}
But it is absolutely unclear for me. Could anyone explain it in a simple way?