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

时间:2021-12-30 15:37:36

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

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

10 个解决方案

#1


24  

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中任何事物的态度形成对比。

#2


16  

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中的源文件导入。另一个原因是合理的源尺寸。

#3


8  

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

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

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

#4


3  

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".

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

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文件)进行额外的传递,以找出哪些类在哪里,这将使编译更慢。

#5


2  

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)如果有主要方法而没有公共类,那么任何名称

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

(3)如果有主要方法和公共课,则主要方法应该在公共课中

(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.

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

#6


2  

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

file1.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文件应该有一个公共类的限制,以便外部世界可以访问每个公共类。

#7


1  

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. 同一个包中的任何两个Java类或接口都不能具有相同的名称。文件名会发生冲突。
  3. Any two Java packages in the same parent package could not have the same name. Folder paths would conflict.
  4. 同一父包中的任何两个Java包都不能具有相同的名称。文件夹路径会发生冲突
  5. A class has visibility to all classes in the same package without modification to the classpath.
  6. 类可以看到同一个包中的所有类,而无需修改类路径。

#8


0  

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.

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

#9


0  

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文件中只能有一个公共类,因为java文件的名称与公共类的名称相同。显然我们不能拥有一个具有两个不同名称的文件。

#10


0  

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函数。因此,编译器可以访问(公共)类并且没有任何混淆/限制。将类名命名为类文件。此外,由于此类包含main(),因此执行类文件将提供正确的流

#1


24  

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中任何事物的态度形成对比。

#2


16  

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中的源文件导入。另一个原因是合理的源尺寸。

#3


8  

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

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

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

#4


3  

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".

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

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文件)进行额外的传递,以找出哪些类在哪里,这将使编译更慢。

#5


2  

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)如果有主要方法而没有公共类,那么任何名称

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

(3)如果有主要方法和公共课,则主要方法应该在公共课中

(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.

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

#6


2  

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

file1.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文件应该有一个公共类的限制,以便外部世界可以访问每个公共类。

#7


1  

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. 同一个包中的任何两个Java类或接口都不能具有相同的名称。文件名会发生冲突。
  3. Any two Java packages in the same parent package could not have the same name. Folder paths would conflict.
  4. 同一父包中的任何两个Java包都不能具有相同的名称。文件夹路径会发生冲突
  5. A class has visibility to all classes in the same package without modification to the classpath.
  6. 类可以看到同一个包中的所有类,而无需修改类路径。

#8


0  

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.

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

#9


0  

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文件中只能有一个公共类,因为java文件的名称与公共类的名称相同。显然我们不能拥有一个具有两个不同名称的文件。

#10


0  

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函数。因此,编译器可以访问(公共)类并且没有任何混淆/限制。将类名命名为类文件。此外,由于此类包含main(),因此执行类文件将提供正确的流