包Java文件命名限制
实际上,我正在浏览Java语言规范,发现了一个奇怪的句子,我无法从中得出结论。因为我不是以英语为母语的人
If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
• The type is referred to by code in other compilation units of the package in which the type is declared.
• The type is declared public (and therefore is potentially accessible from code in other packages).
有人能举例说明用粗体标出的那一行吗。提前谢谢你
# 1 楼答案
在包
com.example
中有类CustomObject
这个类可以从同一个包中的另一个类访问,注意,因为CustomObject是公共类,所以它可以从项目中的任何类访问。如果它被声明为
class CustomObject
,那么它只能在包com.example
中访问# 2 楼答案
在Java中,编译单元本质上是一个文件
如果声明的类型(例如类)在另一个文件中使用,而不是在中声明(通常是这种情况),那么如果找不到以类型名+扩展名命名的文件,则是编译错误
例如:当导入类或其他类型时,Java必须能够将其包+名称转换为必须存在的文件名