Home > Java > javaTutorial > What is the purpose of 'export' clause in module-info file in Java 9?

What is the purpose of 'export' clause in module-info file in Java 9?

WBOY
Release: 2023-09-03 20:21:05
forward
1207 people have browsed it

在Java 9中,module-info文件中的"export"子句有什么用途?

Module is a combination of code and data, it has a name and is declared to other modules A dependency module that exports a package containing public types accessible outside the module and specifies the services it uses or the service implementations it provides. All of this is specified in the module-info.java file, which is included in the module's root directory.

There are two types of "export" clauses that can be used in the "module-info.java" file.

1) Export: By default, a module's type public is no longer visible outside the module. In order to make the public types of a given package visible to other modules, we must export this package. We have to remember that we are at the package level, not the unit level of the type. However, subpackages are not exported.

We need to allow other modules to use the classes and interfaces of the package tp.com.tutorialspoint.model, we can write like this:

<strong>module com.tutorialspoint.model {
   exports tp.com.tutorialspoint.model;
}</strong>
Copy after login

Understand that a package can only appear in This is very important in a module. Otherwise, we will get an error like this:

<strong>Error:(1, 1) java: package exists in another module:</strong>
Copy after login

2) Export to : We can strengthen our Security is provided by reducing the visibility of certain packages to a limited list of modules: only listed modules can access these classes.

<strong>module com.tutorialspoint.model {
   exports tp.com.tutorialspoint.model
      to com.tutorialspoint.gui;
}</strong>
Copy after login

The above is the detailed content of What is the purpose of 'export' clause in module-info file in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template