Modules   «Prev  Next»

Enabling Access Between Modules

In the previous lessons we were introduced to the module-info.java file, and it was required to configure either
  1. Configure a jar
  2. exploded directory

as a module.
At a minimum, a module directive file(module-info.java) for a module named ‘NamedModule’ has the form:
module NamedModule {} 

the official specification for the module declaration is shown below :
ModuleDeclaration:
{Annotation} [open]module Identifier {. Identifier} {{ ModuleDirective}} 

From the Java specification the following directives should populate
{ ModuleDirective} 
are as follows There are 3 others (opens, uses and provides) which are not part of the first exam objective, and we will not be discussing here.
examples of the requires keywords
examples of the requires keywordss

requires.org.module.a;
requires java.logging;
requires transitive org.module.d
requires static.org.module.e

  1. a module specified in the requires directive is 1 which exports packages that the current module may or does have a dependency on The current module is set to be read the module specified in the required directive
  2. A "requires directive" with the transitive modifier allows any module which requires the current module to have an implicit requires directive on the specified module
  3. A static directive requires the specified module at compile time but it's optional at runtime.

Two types of exports: 1) qualified, 2) unqualified.
Two types of exports: 1) qualified, 2) unqualified

The two keyword makes an exports directive qualified and will be followed by a comma delimited list of modules that are termed friends of the current module friends of a module have access to public and protected types and the public and protected members of those types of the exported package no other modules will have access you limit the exposure of the exported package types to its friends .
We are going to walk through each of these directives in IntelliJ. org.module.global Cut and paste the code for public class ApplicationConstants {
Two methods, 1) addCounter 2) getCounter We are going to expose this package to any other module that requires it. by changing the module-info.java file.
module org.module.global {
}

We are exposing this package to any other module that requires it. Right mouse click on
org.pkg.appglobals
and select Build Module 'org.module.global'
jdeps --module-path out/production -m org.module.global

D:\ModuleTesting>jdeps --module-path out/production -m org.module.global
org.module.global
 [file:///D:/ModuleTesting/out/production/org.module.global/]
   requires mandated java.base (@11.0.8)
org.module.global -> java.base
   org.pkg.appglobals                                 -> java.lang                                          java.base

D:\ModuleTesting >java --module-path out/production --describe-module org.module.global

org.module.global file:///D:/ModuleTesting/out/production/org.module.global/
exports org.pkg.appglobals
requires java.base mandated

Let us create a second module that will contain utility code. Intellij wants us to include org.module.global; in its dependencies.
In the next video, we will implement the interface in a 1) separate class 2) separate Module.