是否必须在className.java中编写类className? [英] Is it necessary that class className must be coded in className.java?
问题描述
我正在使用maven来构建项目并且编译失败,因为我在 Test.java
中放了类 Test2
,
I'm using maven to build the project and compile failed because I put class Test2
in Test.java
,
但这是因为maven还是因为java本身不支持这个?
but is it because of maven or simply because java itself doesn't support this?
BTW,如何用eclipse打开maven项目?
BTW,how can I open a maven project with eclipse?
推荐答案
A public
名为 ClassName
的类必须(在大多数情况下)在名为 ClassName.java
的文件中定义。
A public
class called ClassName
must (in most cases) be defined in a file called ClassName.java
.
编辑
虽然这不是Java语言的要求,但大多数(如果不是全部) Java的实现,上面提到的类名和文件名之间的关系必须保持,否则将导致编译器错误。
Although this is not a requirement of the Java language, most (if not all) implementations of Java, the above mentioned relationship between the class name and file name must hold, or a compiler error will result.
(有关更多信息,请参阅 Jon Skeet的回答。)
(For more information, please refer to Jon Skeet's answer.)
re通过阅读第7.2节:软件包可以找到这方面的内容 Java语言规范,第三版。
在本节中,它描述了如何将目录结构映射到 Java中的完全限定类名,这导致要求 .class
包含类的字节码的文件必须位于类似于完全限定类名的路径中。
In the section, it describes how the directory structure can be mapped to a fully-qualified class name in Java, which leads to a requirement that the .class
file which contains the bytecode for a class must reside in a path that resembles that of the fully-qualified class name.
原始答案错误地声明命名方案是一项要求已被编辑。
这篇关于是否必须在className.java中编写类className?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!