java - Java 文件名中应该或必须

标签 java class class-names

<分区>

我正在学习 Java,刚遇到 2 条指令:

  1. 一个类必须有一个匹配的文件名
  2. 记住 java 文件的名称应该与类名匹配

问:

应该 (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/

相关文章:

java - springboot junit测试使用websocket时,它不起作用

java - 如何从静态类更新文本框?

java - 如何使用不同类中返回的值?

jaxb - 为 JAXB 生成的类添加前缀

java - 将 Java 数组中任意范围的元素设置为 null 的最快方法是什么?

java - 如何为 MyLocationOverlay 中的 "you are here"点使用自定义位图?

一个类的c++对象想要在另一个类中使用对象的方法

c++ - 从模板中的另一个类名派生一个类名

c++ - 可以在编译时隐式引用类名吗?

java - Spring PageableArgumentResolver 已弃用,如何使用 PageableHandlerMethodArgumentResolver?