System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə71/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   67   68   69   70   71   72   73   74   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

NOTE
Information about reflection can be found in Java: The Complete Reference, Eleventh
Edition (Oracle Press/McGraw­Hill Education, 2019).
The opens Statement
It is possible for a module to open a specific package for run­time access by other
modules and for reflective access rather than opening an entire module. To do so, use
the opens statement, shown here:
opens packageName;
Here, packageName specifies the package to open. It is also possible to include a to
clause, which names those modules for which the package is opened.
It is important to understand that opens does not grant compile­time access. It is used
only to open a package for run­time and reflective access. One other point: an opens
statement cannot be used in an open module. Remember, all packages in an open
module are already open.
requires static
As you know, requires specifies a dependence that, by default, is enforced both during
compilation and at run time. However, it is possible to relax this requirement in such a


way that a module is not required at run time. This is accomplished by use of the static
modifier in a requires statement. For example, this specifies that mymod is required
for compilation, but not at run time:
In this case, the addition of static makes mymod optional at run time. This can be
helpful in a situation in which a program can utilize functionality if it is present, but not
require it.
CONTINUING YOUR STUDY OF MODULES
The preceding discussions have introduced and demonstrated the core elements of
Java’s module system. They are the features that are directly supported by keywords in
the Java language. Thus, they are the features about which every Java programmer
should have at least a basic understanding. As you might guess, the module system
provides additional features that you will want to learn about as you advance in your
study of Java. A good place to begin is with javac and java. Both have more options
related to modules.
Here are some other areas that you will want to explore. Beginning with JDK 9, the
JDK includes the jlink tool that assembles a modular application into a run­time image
that has only those modules related to the application. This saves both space and
download time. A modular application can be packaged into a JAR file. (JAR stands for
Java ARchive. It is a file format typically used for application deployment.) As a result,
the jar tool now has options that support modules. For example, it can now recognize a
module path. A JAR file that contains a module­info.class file is called a modular
JAR file. For specialized advanced work with modules, you will want to learn about
layers of modules, automatic modules, and the technique by which modules can be
added during compilation or execution.
In conclusion, modules are expected to play an important role in Java programming.
Although their use is not required at this time, they offer important benefits for
commercial applications that no Java programmer can afford to ignore. It is likely that
module­based development will be in nearly every Java programmer’s future.
Ask the Expert


Q
: I have heard the term module graph used in discussions of modules. What
does it mean?
A
: During compilation, the compiler resolves the dependence relationships
between modules by creating a module graph that represents the dependences. The
process ensures that all dependences are resolved, including those that occur
indirectly. For example, if module A requires module B and B requires module C,
then the module graph will contain module C even if A does not use it directly.
Module graphs can be depicted visually in a drawing to illustrate the relationship
between modules, and you will likely encounter one as you continue on in Java.
Here is a simple example. It is the graph for the first module example in this
chapter. (Because java.base is automatically included, it is not shown in the
diagram.)
In Java, the arrows point from the dependent module to the required module.
Thus, a drawing of a module graph depicts what modules have access to what other
modules. Frankly, only the smallest applications can have their module graphs
visually represented because of the complexity typically involved in many
commercial applications.

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   67   68   69   70   71   72   73   74   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə