我写了我的第一个Java程序,并且由于某些原因没有编译
这是我尝试过运行的代码
public class main
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
,它给了我这个错误:
tempcoderunnerfile.java:1:错误:class main是公共的,应在名为main.java的文件中声明。 我不知道
我尝试修改的tbh是什么,它看起来像是他们给出的例子。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,编译器告诉您怎么了。它说:“班级是公开的,应在名为main.java的文件中声明”。
Java有一个命名规则,即Java文件中的类需要匹配该文件名。
示例1:
文件名 - > file.java
该文件中的
:确实违反了该规则(类名称不等于文件名)
示例2:
filename -> main.java
该文件内部的
:遵循该规则(类名称确实等于文件名),
也适用于初学者:
对于基本代码 /命名约定,这是一个很好的快速阅读。
basically the compiler tells you whats wrong. It says, "class main is public, should be declared in a file named main.java".
Java has a naming rule, that a class inside a java file needs to match that file name.
Example 1:
Filename -> File.java
inside that file:
does violate that rule (class name does not equal the file name)
Example 2:
Filename -> Main.java
inside that file:
follows that rule (class name does equal the file name)
Also for beginners:
This is a good quick read for basic code / naming conventions.
https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html
尝试命名您的文件“ main.java”
try naming your file, "main.java"