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
WBOYforward
2023-09-03 20:21:051208browse

在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>

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>

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>

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