Home>Article>Java> 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
WBOY forward
2023-09-03 20:21:05 1080browse

在Java 9中,module-info文件中的

Moduleis a combination ofcodeanddata, 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 themodule-info.javafile, 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 typepublicis no longer visible outside the module. In order to make the public types of a given package visible to other modules, we mustexportthis 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 packagetp.com.tutorialspoint.model, we can write like this:

module com.tutorialspoint.model { exports tp.com.tutorialspoint.model; }

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

Error:(1, 1) java: package exists in another module:

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.

module com.tutorialspoint.model { exports tp.com.tutorialspoint.model to com.tutorialspoint.gui; }

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete