为什么 Java 文件中只有 1 个公共类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3578490/
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-14 02:04:15  来源:igfitidea点击:

Why only 1 public class in Java file

java

提问by Jenesh

In any Java file, why can we have only one public class whose name is same as the Java file name?

在任何Java文件中,为什么我们只能有一个名称与Java文件名相同的公共类?

回答by Timo Westk?mper

According to this source, it is for efficient compilation :

根据这个来源,它是为了高效编译:

In the sidebar it explains why: "This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"

It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units (.java files) to figure out what classes were where, and that would make the compilation even slower.

在侧边栏中解释了原因:“编译器尚未强制执行此限制,尽管它对于有效的包导入是必要的”

很明显 - 就像大多数事情一样,一旦你知道设计原因 - 编译器必须额外通过所有编译单元(.java 文件)来找出哪些类在哪里,这会使编译更慢.

The same applies also for imports of source files in IDEs. Another reason would be reasonable source sizes.

这同样适用于在 IDE 中导入源文件。另一个原因是合理的源大小。

回答by Mike Baranczak

It forces all Java code to be organized in a certain way, which in the long run helps improve code readability.

它强制以某种方式组织所有 Java 代码,从长远来看,这有助于提高代码的可读性

The Java designers chose a strict approach that enforces their idea of good design practices, and this is part of that theme. Contrast that with the anything-goes attitude in Perl.

Java 设计者选择了一种严格的方法来强制执行他们的良好设计实践的想法,这是该主题的一部分。将其与 Perl 中的任何事情的态度形成对比。

回答by spbfox

These are the rules. Although it is not quite true. You can define internal classes inside you "main" class like this:

这些是规则。虽然这并不完全正确。您可以在“主”类中定义内部类,如下所示:

public class A {  
   public class B {  
       ...  
   }  
}

回答by gawi

It enables a more efficient lookup of source (.java) and compiled (.class) files during compilation (import directive) and a more efficient classloading during execution. The idea being: if you know the name of a class, you know where it should be found for each classpath entry. No indexing required.

它可以在编译(导入指令)期间更有效地查找源 ( .java) 和已编译 (.class) 文件,并在执行期间更有效地加载类。这个想法是:如果你知道一个类的名称,你就知道应该在哪里为每个类路径条目找到它。不需要索引。

回答by Aaron

Java utilizes this convention to find class/interface bytecode by starting at the classpath and scanning for the package hierarchy in subdirectories. Filesystem representation of this hierarchy also enforces some basic rules.

Java 利用此约定通过从类路径开始并扫描子目录中的包层次结构来查找类/接口字节码。此层次结构的文件系统表示还强制执行一些基本规则。

  1. Any two Java classes or interfaces in the same package cannot have the same name. File names would conflict.
  2. Any two Java packages in the same parent package could not have the same name. Folder paths would conflict.
  3. A class has visibility to all classes in the same package without modification to the classpath.
  1. 同一个包中的任何两个 Java 类或接口不能具有相同的名称。文件名会冲突。
  2. 同一个父包中的任何两个 Java 包不能具有相同的名称。文件夹路径会冲突。
  3. 一个类可以看到同一个包中的所有类,而无需修改类路径。

回答by Joel

Courtesy of Dr Heinz Kabutz and his excellent newsletter....

感谢 Heinz Kabutz 博士和他出色的时事通讯......

Why is each public class in a separate file?

This is a question that I have frequently been asked during my courses. Up to now I have not had a good answer to this question. In section 1, we read: "Although each Oak compilation unit can contain multiple classes or interfaces, at most one class or interface per compilation unit can be public".

In the sidebar it explains why: "This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"

It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units (.java files) to figure out what classes were where, and that would make the compilation even slower.

为什么每个公共类都在一个单独的文件中?

这是我在课程中经常被问到的问题。到目前为止,我对这个问题还没有一个好的答案。在第 1 节中,我们读到:“虽然每个 Oak 编译单元可以包含多个类或接口,但每个编译单元最多只能有一个类或接口是公共的”。

在侧边栏中解释了原因:“编译器尚未强制执行此限制,尽管它对于有效的包导入是必要的”

很明显 - 就像大多数事情一样,一旦你知道设计原因 - 编译器必须额外通过所有编译单元(.java 文件)来找出哪些类在哪里,这会使编译更慢.

回答by Venkataswamy

We can have only one top level public either class or interface in any java compilation unit ( .java source file ).

在任何 Java 编译单元(.java 源文件)中,我们只能有一个顶级公共类或接口。

But there can be any number of default classes/interfaces per src file.

但是每个 src 文件可以有任意数量的默认类/接口。

why:

为什么:

JLS leaves the option to the java compiler. And most of the compiler implementations force to have file name same as :

JLS 将选项留给 java 编译器。并且大多数编译器实现强制具有与以下内容相同的文件名:

(1) the public class/interface name

(1) 公共类/接口名称

(2) if there is a main method and no public class then any name

(2) 如果有一个 main 方法而没有 public 类,那么任何名称

(3) If there is main method and public class then main method should be in that public class

(3) 如果有 main 方法和 public 类,那么 main 方法应该在那个 public 类中

(4) if there is no public class and no main method then any valid name which may or may not be matching with the class/interface names in the file.

(4) 如果没有公共类和主方法,那么任何有效的名称可能与文件中的类/接口名称匹配,也可能不匹配。

From (2): If two public classes allowed, we should give the file two names which is terribly meaningless to file system. From (3): If two public classes allowed, we should have two main methods which is terribly meaningless to java

来自(2):如果允许两个公共类,我们应该给文件两个名字,这对文件系统来说是非常没有意义的。来自(3):如果允许两个公共类,我们应该有两个对java来说毫无意义的主要方法

Hence a Java source file can have one only public class.

因此,一个 Java 源文件只能有一个公共类。

I think the above 4 points are forced by compiler to make the job of both compiler and jvm to find particular java source file or class file easy-so-quick for the compilation/loading/linking. Java has such built in restrictions which developers should follow to have better programming.

我认为上述 4 点是编译器强制要求编译器和 jvm 共同寻找特定的 java 源文件或类文件的工作,以便为编译/加载/链接轻松快速。Java 有这样的内置限制,开发人员应该遵循这些限制来进行更好的编程。

Source: My readings and understanding.

资料来源:我的阅读和理解。

回答by Anand Srivastava

I think that this may be a possible reason. There can be only one public class in a java file because the name of java file is same as the name of public class.And obviously we can't have a file with two different names.

我认为这可能是一个可能的原因。一个java文件中只能有一个public class,因为java文件名和public class名相同。显然我们不能有两个不同名字的文件。

回答by Anand Srivastava

To have an understanding between the compiler and the programmer.It is a rule that the source code must have atmost one public class and that class must contain the main function.So without any confusion/restriction the compiler can access(public) the class and name the class name to the class file.Also since this class contains the main(), executing the class file will give correct flow

为了让编译器和程序员之间有一个理解。源代码最多必须有一个公共类,并且该类必须包含主函数,因此编译器可以在没有任何混淆/限制的情况下访问(公共)类和将类名命名为类文件。此外,由于此类包含 main(),因此执行类文件将提供正确的流程

回答by prat

To understand the basic reason behind these restrictions, let's suppose compiler doesn't give compile error for not naming file name same as public class name.

为了理解这些限制背后的基本原因,让我们假设编译器不会因为没有将文件名命名为公共类名而给出编译错误。

Suppose there is a package A

假设有一个包 A

        A
      /   \
file1.java   file2.java

file1.java

文件1.java

package A;

class file1
{
  public static void main(String args[])
  {

  }
}

public class file3
{
 public static void main(String args[])
 {

 }
}

Now as we know that a public class can also be accessed outside the package, now it will become the responsibility of a developer to make it accessible to the outside world. Let's see how:

现在我们知道公共类也可以在包外访问,现在开发人员有责任使其外部世界可以访问。让我们看看如何:

Suppose package A is containing only Java files(no class files) and some class outside the package A tries to access public class file3, compiler will first try to find file3.class ( not available ), then it will try to find file3.java ( not available ). So even though file3 class is public in nature, it is not visible to the outside world. So if a compiler puts the restriction that if a file is containing public class, it should be named same as the public class name, then above issue can be resolved and the developer won't have to think of exposing public class to the outside world.

假设包 A 只包含 Java 文件(没有类文件)并且包 A 之外的某个类尝试访问公共类 file3,编译器将首先尝试查找 file3.class(不可用),然后它会尝试查找 file3.java ( 无法使用 )。因此,即使 file3 类本质上是公共的,但它对外界是不可见的。所以如果编译器限制如果文件包含公共类,它应该与公共类名称相同,那么上述问题就可以解决,开发人员就不必考虑将公共类暴露给外界.

Compiler also puts the restriction that there should be atmost one public class per Java file, so that every public class can be accessed by the outside world.

编译器还限制了每个 Java 文件最多只能有一个公共类,以便外部世界可以访问每个公共类。