"公共类型<<classname>>必须在其自己的文件中定义"Eclipse 中的错误 [英] "The public type <<classname>> must be defined in its own file" error in Eclipse

查看:12
本文介绍了"公共类型<<classname>>必须在其自己的文件中定义"Eclipse 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码:

package staticshow;


public class StaticDemo {
  static int a = 3;
  static int b = 4;

  static {
    System.out.println("Voila! Static block put into action");
  }

  static void show() {
    System.out.println("a= " + a);
    System.out.println("b= " + b);
  }
}

public class StaticDemoShow {
  public static void main() {
    StaticDemo.show(); 
  }
}

我收到错误消息:

The public type StaticDemo must be defined in its own file

第一行错误public class StaticDemo {.为什么会发生这种情况,我该如何解决?请注意,我的项目名称是 StaticDemoShow,包名称是 staticshow,类名称在代码中给出.

error in the very first line public class StaticDemo {. Why is it happening and how can I resolve it? Note that my project name is StaticDemoShow, package name is staticshow and class names are as given in the code.

编辑- 仅将一个类设为公开或将两个类设为默认值后,出现错误选择不包含主要类型".现在我该怎么办?

EDIT- After making just one class public or both the classes default, I am getting the error "Selection does not contain a main type". Now what should I do?

推荐答案

如果 .java 文件包含顶级(非嵌套)public 类,它必须具有与那个公共类同名.所以如果你有像 public class A{...} 这样的类,它需要放在 A.java 文件中.因此,我们不能在一个 .java 文件中包含两个公共类.

If .java file contains top level (not nested) public class, it has to have the same name as that public class. So if you have class like public class A{...} it needs to be placed in A.java file. Because of that we can't have two public classes in one .java file.

如果允许有两个公共类,那么除了公共 A 类文件还将包含 public class B{} 它需要来自 A.java 文件 命名为 B.java 但文件不能有两个(或多个)名称(至少在所有系统上)可以运行哪个Java).

If having two public classes would be allowed then, and lets say aside from public A class file would also contain public class B{} it would require from A.java file to be also named as B.java but files can't have two (or more) names (at least in all systems on which Java can be run).

因此假设您的代码位于 StaticDemoShow.java 文件中,您有两个选择:

So assuming your code is placed in StaticDemoShow.java file you have two options:

  1. 如果你想在同一个文件中包含其他类,让它们成为非公开的(缺少可见性修饰符将代表 default/package-private 可见性)

 class StaticDemo { // It can no longer public

     static int a = 3;
     static int b = 4;

     static {
         System.out.println("Voila! Static block put into action");
     }

     static void show() {
         System.out.println("a= " + a);
         System.out.println("b= " + b);
     }

 }

 public class StaticDemoShow { // Only one top level public class in same .java file
     public static void main() {
         StaticDemo.show();
     }
 }

  • 将所有公共类移动到它们自己的 .java 文件中.因此,在您的情况下,您需要将其拆分为两个文件:

  • Move all public classes to their own .java files. So in your case you would need to split it into two files:

    • StaticDemo.java

    • StaticDemo.java

      public class StaticDemo { // Note: same name as name of file
    
          static int a = 3;
          static int b = 4;
    
          static {
              System.out.println("Voila! Static block put into action");
          }
    
          static void show() {
              System.out.println("a= " + a);
              System.out.println("b= " + b);
          }
    
      }
    

  • StaticDemoShow.java

  • StaticDemoShow.java

      public class StaticDemoShow { 
          public static void main() {
              StaticDemo.show();
          }
      }
    

  • 这篇关于"公共类型<<classname>>必须在其自己的文件中定义"Eclipse 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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