<分区>
我正在学习 Java,刚遇到 2 条指令:
- 一个类必须有一个匹配的文件名
- 记住 java 文件的名称应该与类名匹配
问:
应该 (2) LIKE or DIFFERENT 必须 在 (1) ?? -> (1), (2).
什么是正确的??
标签 java class class-names
<分区>
我正在学习 Java,刚遇到 2 条指令:
问:
应该 (2) LIKE or DIFFERENT 必须 在 (1) ?? -> (1), (2).
什么是正确的??
最佳答案
如评论中所述,这并不像一开始看起来那么清楚,部分原因是 JLS 不需要将包存储在文件系统中:
7.6. Top Level Type Declarations :
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:
[...]
所以你的答案基本上是:
根据您的编译器,它可能是“必须的”。
关于java - Java 文件名中应该或必须,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53869927/