Table of Contents
1. What is Java module?
2. Module export package
3. Module import package
4. The significance of Java module
5. Example
The first module
The second module
Home Java javaTutorial Java9 new feature Module modular programming method

Java9 new feature Module modular programming method

May 19, 2023 pm 01:51 PM
java module

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{
  ....
 }
Copy after login

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;
 }
Copy after login

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;}
Copy after login

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
Copy after login

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);
  }
}
Copy after login

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);
  }
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

Use package:com.zimug in the imported module common.widget. The path of this code file:

##modules-example/src/data.widget/com/example/Component.java

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
Compile and execute normally, the results are as follows:

Test Object
Copy after login

Attempting to use package code that has not been exported

Because package "org.jwidgets" has not been exported by the "common.widget" module, another module "data.widget" cannot use the package package Class

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");
  }
}
Copy after login

The compilation error message is as follows:

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
Copy after login
Even if it is declared as public, but the class under the unexported package cannot be access.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

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

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

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

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

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

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

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

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

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

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

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

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

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.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

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

See all articles