Java Eclipse中的“公共类型<<classname>>必须在其自己的文件中定义”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19814713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:26:19  来源:igfitidea点击:

"The public type <<classname>> must be defined in its own file" error in Eclipse

javaeclipse

提问by Mistu4u

I have written the following code:

我编写了以下代码:

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(); 
  }
}

I am getting the error message:

我收到错误消息:

The public type StaticDemo must be defined in its own file

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 staticshowand class names are as given in the code.

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

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?

编辑- 在只公开一个类或两个类默认后,​​我收到错误“选择不包含主类型”。现在我该怎么办?

采纳答案by Raju Sidda

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

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

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

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

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

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

  1. If you want to have other class in same file make them non-public (lack of visibility modifier will represent default/package-privatevisibility)

    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();
        }
    }
    
  2. Move all public classes to their own .javafiles. So in your case you would need to split it into two files:

    • 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

      public class StaticDemoShow { 
          public static void main() {
              StaticDemo.show();
          }
      }
      
  1. 如果您想在同一个文件中包含其他类,请将它们设为非公开(缺少可见性修饰符将代表默认/包私有可见性)

    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();
        }
    }
    
  2. 将所有公共类移动到它们自己的.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);
          }
      
      }
      
    • 静态演示程序

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

回答by Ankit Rustagi

Save this class in the file StaticDemo.java. Also you cant have more than one public classes in one file.

将此类保存在文件StaticDemo.java 中。此外,一个文件中不能有多个公共类。

回答by Paul Samsotha

Cant have two public classes in same file

不能在同一个文件中有两个公共类

   public class StaticDemo{

Change to

改成

   class StaticDemo{

回答by Areo

Java rule : One publicclass in one file.

Java 规则:public一个文件中的一个类。

回答by Shoaib Chikate

You can have only one public class in a file else you will get the error what you are getting now and name of file must be the name of public class

一个文件中只能有一个公共类,否则你会得到你现在得到的错误,文件名必须是公共类的名称

回答by user2960876

You can't use 2 public class instances, you need to use one. Try using class (name) instead of public class (name)

您不能使用 2 个公共类实例,您需要使用一个。尝试使用类(名称)而不是公共类(名称)

回答by Sage

error in the very first line public class StaticDemo {

第一行中的错误 public class StaticDemo {

Any Class Awhich has access modifier as publicmust have a separate source file as A.javaor A.jav. This is specified in JLS 7.6 section:

任何A具有访问修饰符 as 的类都public必须有一个单独的源文件 as A.javaor A.jav这在 JLS 7.6 部分中指定

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:

  • The type is referred to by code in other compilation units of the package in which the type is declared.

  • The type is declared public(and therefore is potentially accessible from code in other packages).

当且仅当包存储在文件系统中时(第 7.2 节),如果在由类型名称组成的名称下的文件中找不到类型,则主机系统可以选择强制执行限制,即它是编译时错误如果以下任一情况为真,则加上扩展名(例如 .java 或 .jav):

  • 该类型由声明该类型的包的其他编译单元中的代码引用。

  • 该类型被声明为public(因此可以从其他包中的代码访问)。

However, you may have to remove publicaccess modifier from the Class declaration StaticDemo. Then as StaticDemoclass will have no modifier it will become package-private, That is, it will be visible only within its own package.

但是,您可能必须public从 Class 声明中删除访问修饰符StaticDemo。然后,由于StaticDemoclass 没有修饰符,它将变为package-private,也就是说,它仅在其自己的包中可见。

Check out Controlling Access to Members of a Class

查看控制对类成员的访问

回答by Mistu4u

I had two significant errors in my program. From the other answers, I learned in a single java program, one can not declare two classes as "public". So I changed the access specifier, but got another error as added to my question as "EDIT" that "Selection does not contain a main type". Finally I observed I forgot to add "String args[]" part in my main method. That's why the code was not working. After rectification, it worked as expected.

我的程序中有两个重大错误。从其他答案中,我在单个 Java 程序中了解到,不能将两个类声明为“公共”。因此,我更改了访问说明符,但在我的问题中添加了另一个错误,即“编辑”,即“选择不包含主要类型”。最后我发现我忘记在我的主要方法中添加“String args[]”部分。这就是代码不起作用的原因。整改后,按预期运行。