错误:应在名为 X.java 的文件中声明类 X 是公共的 [英] Error: class X is public should be declared in a file named X.java

查看:29
本文介绍了错误:应在名为 X.java 的文件中声明类 X 是公共的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,但我收到此编译器错误:

I am trying to write a program, but I'm getting this compiler error:

Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java
public class WeatherArray {
       ^
1 error

我检查了我的文件名,我的公共类与我的 .java 文件相同.

I have checked my file names, and my public class is the same as my .java file.

我该如何解决这个问题?

How can I fix this?

这是我的代码:

public class WeatherArray {
    public static void main(String[] args) {
        // ...
    }
}

推荐答案

公共类的名称必须与放置它的 .java 文件的名称相匹配(例如 public class Foo{} 必须放在 Foo.java 文件中).所以要么:

Name of public class must match the name of .java file in which it is placed (like public class Foo{} must be placed in Foo.java file). So either:

  • 将您的文件Main.java重命名为WeatherArray.java
  • classpublic class WeatherArray { 重命名为 public class Main {
  • rename your file from Main.java to WeatherArray.java
  • rename the class from public class WeatherArray { to public class Main {

这篇关于错误:应在名为 X.java 的文件中声明类 X 是公共的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆