Java9 new feature Module modular programming method
The Java language introduced an important concept in its version 9 - module. If you are familiar with the modular management of JavaScript code, you should feel familiar when you see the modular management of Java 9.
1. What is Java module?
Module introduces a higher level of Java code grouping, similar to packages in Java. Each such group (module) contains many sub-packages. Declare the folder and its subfolders as a module by adding the file module-info.java to the root of a module's source code file package. The syntax of the file is as follows:
module xxx.yyy{ .... }
where xxx.yyy is the name declared by the module module, not the package name.
2. Module export package
The file module-info.java can specify which packages under the module are visible and accessible to the outside world. This function is implemented through a new keyword exports
.
module xxx.yyy{ exports com.zimug.java9; }
com.zimug.java9
represents a package.
It should be noted that even if the classes in a given package are public, if their package is not explicitly exported through "exports", they are not visible outside the module ( This is true both at compile time and at run time).
3. Module import package
If another module wants to use the classes in the exported package, you can use the requires
keyword in its module -info.java file to import (read) the package package of the target module.
module def.stu{ requires xxx.yyy;}
4. The significance of Java module
The author believes that the introduction of the module modular management system in Java 9 is mainly due to security considerations. More than 90% of vulnerabilities in Java code are caused by reflection and insufficient access control granularity. The modular system of Java 9 can solve this problem. Java 9 modularity provides a higher level of visibility and accessibility control of Java code.
As an example, when we mark a class as private, it means that it is an inner class. There are only two modifiers for external classes: public and default. This also means a problem. We originally planned to use some public classes within the scope defined by the jar package, but the result is that any project that introduces this jar can use all the public class codes in this jar.
That is to say, our original intention was to provide public access within a limited scope, but the result was unlimited public access. After the introduction of Java 9 modularization, limited range of code public access rights can be achieved, and the code publicity is divided into: limited range of public access outside the moduleandinside the module Public access.
5. Example
In this example, I will create two modules "common.widget" and "data.widget" and place them in a single folder "modules-examples /src". The file "module-info.java" will be placed under the root folder of each module.
The file and directory format is as follows:
D:\modules-example>tree /F /A \---src +---common.widget | | module-info.java | | | +---com | | \---zimug | | RendererSupport.java | | | \---org | \---jwidgets | SimpleRenderer.java | \---data.widget | module-info.java | \---com \---example Component.java
The first module
This code file directory:
modules-example/src/common.widget/ org/jwidgets/SimpleRenderer.java.
This package is not exported in the following text.
package org.jwidgets; public class SimpleRenderer { public void renderAsString(Object object) { System.out.println(object); } }
This code file directory:
modules-example/src/common.widget/com/zimug/RendererSupport.java.
This package will be exported later.
package com.zimug; import org.jwidgets.SimpleRenderer; public class RendererSupport { public void render(Object object) { new SimpleRenderer().renderAsString(object); } }
Module export, this code file directory: modules-example/src/common.widget/module-info.java. Only the com.zimug
package is exported, and the org.jwidgets
package is not exported. The exported module name is common.widget
module common.widget{ exports com.zimug; }
The second module
module imports common.widget
, this code file directory: modules- example/src/data.widget/module-info.java
module data.widget { requires common.widget; }
Use package:com.zimug
in the imported module common.widget
. The path of this code file:
##modules-example/src/data.widget/com/example/Component.javaCompile and execute normally, the results are as follows:package com.example; import com.zimug.RendererSupport; public class Component { public static void main(String[] args) { RendererSupport support = new RendererSupport(); support.render("Test Object"); } }Copy after login
Test Object
SimpleRenderer. Let’s make a counterexample to see what happens:
package com.example; import org.jwidgets.SimpleRenderer; public class Component { public static void main(String[] args) { SimpleRenderer simpleRenderer = new SimpleRenderer(); simpleRenderer.renderAsString("Test Object"); } }
D:\modules-example\src\data.widget\com\example\Component.java:3: error: package org.jwidgets is not visible import org.jwidgets.SimpleRenderer; ^ (package org.jwidgets is declared in module common.widget, which does not export it) 1 error
The above is the detailed content of Java9 new feature Module modular programming method. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
