java 9 模块中没有访问说明符和 public 有什么区别?

What is the difference between no access specifier and public in java 9 module?

从java9开始,public访问仅限于它自己的模块。这是否意味着 public 的行为类似于 package protected(无访问说明符)?有人可以澄清一下吗?

Module system 的主要目标之一是提供:

Strong encapsulation, to allow a component to declare which of its public types are accessible to other components, and which are not.

添加它的 #accessibility 方面 -

The Java compiler and virtual machine consider the public types in a package in one module to be accessible by code in some other module only when the first module is readable by the second module, in the sense defined above, and the first module exports that package

因此,为了从另一个模块访问包的 public 类型,被访问的模块需要 export 该包以使其可读。

但这并不一定适用于模块系统中所有类型的模块(例如自动模块)。

Does it mean public acts like a package protected ( no access specifier )?

No,public 类型应该可以被同一模块中的不同包访问,以及何时(如果) 导出后,该包也公开了其他模块的其他包对 public 类型的访问。

package 元素只能在包内访问。模块中的其他包无法访问这些元素。

public 没有 exports 模块中的任何其他包都可以访问。

换句话说:

  • package 元素是 package-local
  • public 没有 exportsmodule-local.

所以,没有 exportspublicpackage 宽。