为什么 Java 中的文件名与公共类名相同?

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

Why are filenames in Java the same as the public class name?

java

提问by Thunderhashy

In Java, the name of the file should be the same as the name of the public classcontained in that file. Why is this a limitation? What purpose does it serve?

在 Java 中,文件名应与该文件中public class包含的名称相同。为什么这是一个限制?它的目的是什么?

采纳答案by Bill K

Java had an interesting approach: where giving a programmer a choice can only degrade the programming experience, remove the choice.

Java 有一个有趣的方法:在给程序员一个选择只会降低编程体验的地方,删除选择。

They did this in quite a few places. Filenames and packages for sure, but also not allowing multiple public classes in a file (never good), not allowing you to split classes between files (Damn hard to work with!), etc.

他们在很多地方都这样做了。文件名和包是肯定的,但也不允许在一个文件中存在多个公共类(永远不会好),不允许您在文件之间拆分类(该死的很难使用!)等。

I really wish they had gone a few further. There is no reason for public variables: I've never needed one, nor have I ever seen a situation where some smart programmer thought one was needed and was actually right.

我真的希望他们走得更远。没有理由使用公共变量:我从来不需要一个,也从未见过一些聪明的程序员认为需要一个并且实际上是正确的情况。

I also wouldn't mind seeing method/class size limitations, but this could get sketchy (it could easily be implemented by code checkers, the problem is typically that the companies that need the most help are the ones that don't know they need help and, therefore, don't use tools like code checkers).

我也不介意看到方法/类大小限制,但这可能会变得粗略(它可以很容易地由代码检查器实现,问题通常是最需要帮助的公司是那些不知道他们需要的公司帮助,因此,不要使用代码检查器之类的工具)。

This isn't stuff that matters to most small teams, but when your team grows and has multiple sites with consultants from India, China, and various other spots throughout the world, You'll start to appreciate the inflexibility.

这对大多数小团队来说并不重要,但是当您的团队成长并拥有多个站点以及来自印度、中国和世界各地其他各个地方的顾问时,您就会开始意识到这种不灵活。



In response to setters/getters comment:

回应 setter/getter 评论:

Java beans were an abomination created by Borland to hack their GUI up, then retrofitted into Java.

Java bean 是 Borland 创建的一个令人憎恶的东西,用来破解他们的 GUI,然后改装成 Java。

Horrid idea--a distraction from OO programming--Getters and setters A) show too much of your implementation and B) make you think in terms of operating on data from another object rather than asking the other object to execute an operation for you. Bad hack for people who can't yet think in OO.

可怕的想法——从 OO 编程中分心——Getter 和 setter A) 显示了太多你的实现,B) 让你思考操作来自另一个对象的数据,而不是要求另一个对象为你执行操作。对于还不能在面向对象中思考的人来说,这是一个糟糕的黑客。

Getters are needed occasionally but shouldn't be added unless seen to be absolutely unavoidable.

偶尔需要使用吸气剂,但不应添加,除非被认为是绝对不可避免的。

Setters should be avoided at all costs. If you absolutely need to externally modify the state after an object is constructed, try to use the builder pattern and protect your setters from being called after any operation has been executed.

应不惜一切代价避免使用二传手。如果您绝对需要在构造对象后从外部修改状态,请尝试使用构建器模式并保护您的 setter 不会在任何操作执行后被调用。

There are obviously exceptions to everything, and many "Getters" are actually critical object business logic, like String.length() which would be required no matter how String was implemented and isn't even implemented by just returning a property--a great case for a "Getter" if you even want to call it that.

一切显然都有例外,许多“Getter”实际上是关键的对象业务逻辑,例如 String.length(),无论 String 是如何实现的,它都是必需的,甚至不是通过返回一个属性来实现的——一个很好的如果你想这样称呼它,那就是“Getter”的情况。

回答by BlueRaja - Danny Pflughoeft

It is just the conventionset by Sun, the makers of Java.
The purpose is organization; the reason is so that everyone who codes in Java will have a consistant way of naming files.

这只是Java 的制造商 Sun 制定的惯例
目的是组织;原因是每个用 Java 编码的人都会有一个一致的文件命名方式。

回答by GuruKulki

To be more specific, the filenameshould have the same name as the public class namein that file, which is the way to tell the JVM that this is what the entry point is for you.

更具体地说,文件名应该与该文件中的公共类名具有相同的名称,这是告诉 JVM 这就是您的入口点的方式。

回答by Andreas Dolk

I was about to say, that it is simply a must. But I looked at the JLS, and it is not that strict. From the JLS's perspective, it is left to the compiler to choose whether to set such a restriction or not.

我正要说,这简直是必须的。但是我看了JLS,并没有那么严格。从 JLS 的角度来看,由编译器选择是否设置这样的限制。

Practically spoken - common compilers do have that restriction, and, as other already explained, it's much easier for the compiler to find a compilation unit or for a classloader to find a class file with such a restriction in place.

实际上说 - 常见的编译器确实有这种限制,并且正如其他人已经解释过的那样,编译器更容易找到编译单元或类加载器更容易找到具有这种限制的类文件。

回答by phisch

Each public class must be in a file where the FileName matches the ClassName and a package where the Packagename represents the Directory structure, written in the dotted-form (slashes become dots, like com/example/app becomes com.example.app).

每个公共类都必须在一个文件中,其中 FileName 与 ClassName 匹配,并且一个包中,其中 Packagename 表示目录结构,以点状形式编写(斜线变成点,如 com/example/app 变成 com.example.app)。

This convention is not random. The compiler has to be able to find the source files and the class loader has to be able to find the implementation. Matching package names and classnames makes this really simple and, more important, fast.

这个约定不是随机的。编译器必须能够找到源文件,而类加载器必须能够找到实现。匹配包名和类名使这变得非常简单,更重要的是,快速。

This convention does not apply to non-public classes. This is because non-public classes have a very limited visibility and can only be used within the package where they are defined. Thus, in both cases, the compiler and the runtime environment have already located the correct files.

此约定不适用于非公共类。这是因为非公共类的可见性非常有限,只能在定义它们的包中使用。因此,在这两种情况下,编译器和运行时环境都已经找到了正确的文件。

回答by Vivek

It is useful in locating the class. i.e. Suppose different file names are allowed and if you have created an instance of a class then the compiler has to search the class in all file instead if the file-names are same as that of the class the performance of locating and using the class is increased. Their might be other reasons too.

它在定位类时很有用。即假设允许不同的文件名,并且如果您创建了一个类的实例,那么编译器必须在所有文件中搜索该类,如果文件名与该类的文件名相同,则定位和使用该类的性能是增加。他们也可能是其他原因。

回答by Venkataswamy

As long as it is not public a class can have a name different from its file name. The class can also have main method. Class file will be generated with the class name but not with the source file name. The class name should be used to execute it.

只要它不是公共的,一个类就可以有一个不同于它的文件名的名称。类也可以有 main 方法。类文件将使用类名而不是源文件名生成。应该使用类名来执行它。

Reason is:default class is package private, thus javac doesn't have to find this source file of this to compiler some other java program from outside of package.

原因是:默认类是包私有的,因此javac不必找到this的这个源文件来编译来自包之外的其他java程序。