concept require in category java

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.
A brief summary of module directives beyond simple requires and exports
Java 9 introduced a new module system in which a module-info.java file names a module and specifies its dependencies (requires) and public API (exports). The requires clause lets you specify dependencies on other modules.

This is an excerpt from Manning's book The Java Module System.
A
module the.name { }block defines a module. The name typically follows the package-naming convention: it should be globally unique by reverting a domain name, it’s all lowercase, and sections are separated by dots (see section 3.1.3 for more—I use shorter names only to make them more amenable for this book). Inside the module block,requiresdirectives express the dependencies between modules, andexportsdirectives define each module’s public API by naming the packages whose public types are to be exported.
Chapter 3 explains how
requiresandexportsdirectives are the basis for readability and accessibility. But these mechanisms are strict: every module has to be explicitly required, all required modules have to be present for the application to compile and launch, and exported packages are accessible to all other modules. This suffices for the majority of use cases, but there’s still a significant portion in which these solutions are too broad.The most obvious use case is optional dependencies, which a module wants to compile against but which aren’t necessarily present at run time. Spring, for example, does this with the Jackson databind library. If you run a Spring application and want to use JSON as a data-transfer format, you can get support for that by dropping in the Jackson artifact. If, on the other hand, that artifact is absent, Spring is still happy—it doesn’t support JSON then. Spring uses Jackson but doesn’t require it.
Regular
requiresdirectives don’t cover this use case, though, because the modules would have to be present for the application to launch. Services can be the solution in some such cases, but using them for all optional dependencies would lead to many awkward and complex implementations. Hence, plainly expressing that a dependency isn’t required at run time is an important feature; section 11.2 shows how the JPMS implements it.
When it comes to the interplay between
requiresdirectives and accessibility, there’s a fine detail to observe: Therequiresdirectives create reads edges but the edges are a prerequisite for accessibility. Doesn’t that beg the question of which other mechanisms can establish readability and thus unlock access to types? This is more than theoretical pondering—approaching the situation from a practical angle, we end up in the same place.Let’s turn back to the ServiceMonitor application, particularly the modules monitor.observer and monitor.observer.alpha. Assume that a new module, let’s call it monitor.peek, wants to use monitor.observer.alpha directly. It has no need for monitor.observer or the service architecture you created in the previous chapter. Can monitor.peek just
requiremonitor.observer.alpha and start using it?ServiceObserver observer = new AlphaServiceObserver("some://service/url"); DiagnosticDataPoint data = observer.gatherDataFromService();It looks like it needs the types
ServiceObserverandDiagnosticDataPoint. Both are in monitor.observer, so what happens if monitor.peek doesn’trequiremonitor.observer? It can’t access its types, resulting in compile errors. As you saw when we discussed the encapsulation of transitive dependencies in section 3.3.2, this is a feature of the module system.Here it’s an impediment, though. Without the types from monitor.observer, monitor.observer.alpha is effectively useless; and every module that wants to use it has to read monitor.observer as well. (This is shown in figure 11.1.) Does every module using monitor.observer.alpha have to
requiremonitor.observer, too?
Figure 11.1 The module peek uses observer.alpha, which uses types from observer in its public API. If peek doesn’t require observer (left), it can’t read its types, making observer.alpha useless. With regular
requiresdirectives, the only way around that is to have peek also require observer (right), which becomes cumbersome when more modules are involved.![]()