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

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

Java有一个有趣的方法 – 给程序员一个select只能降低编程经验,删除select。

他们在很多地方做了这个。 文件名和包是肯定的,但也不允许在一个文件中的多个公共类(从来没有好),不允许你在文件之间拆分类(该死的难以处理!)等

我真的希望他们进一步了。 没有任何公共变数的理由 – 我从来没有需要,也没有见过一个聪明的程序员认为需要一个,其实是正确的情况。

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

这对于大多数小团队来说并不重要,但是当你的团队成长起来,并且拥有来自印度,中国以及世界各地其他地方的顾问的多个网站时,你会开始体会到这种僵化。

在回应setter / getters评论:

Java bean是Borland创build的一个憎恶他们的GUI,然后改装成Java。

Horrid的想法 – 从OO编程中分心 – 吸气剂和吸附剂A)显示太多的实施,并且B)让你从另一个对象的数据上操作,而不是要求另一个对象为你执行一个操作。 对于那些还不能在OO中思考的人来说,糟糕的黑客攻击。

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

安装者应该不惜一切代价避免。 如果在构造对象后绝对需要外部修改状态,请尝试使用构build器模式,并在执行任何操作后保护您的设置器不被调用。

有一些明显的例外,许多“Getters”实际上是关键的对象业务逻辑,比如String.length(),不pipeString是如何实现的,甚至不是通过返回一个属性来实现的 – 一个伟大的如果你想称之为“Getter”的话。

我正要说,这是一个必须 。 但是我看了JLS ,并没有那么严格。 从JLS的angular度来看,编译器会select是否设置这样的限制。

实际上,普通的编译器确实有这样的限制,正如其他人已经解释的那样,编译器find一个编译单元或者一个类加载器来find一个这样的限制的类文件要容易得多。

更具体地说,文件名与该文件中的公共类名相同。 这是告诉JVM这个入口点是什么的方法。

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

每个公共类必须位于FileName与ClassName相匹配的文件中,Packagename代表目录结构,以虚线forms(斜线变为圆点,如com / example / app变为com.example.app)写的包。

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

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

问:那么在C ++的情况下,它是如何工作的?

答:不行。 你必须有一个makefile。 Java中不需要一个。

这在定位课程中很有用。 即假设不同的文件名是允许的,如果你已经创build了一个类的实例,那么编译器必须在所有文件中search类,而不是文件名与类的类相同,定位和使用类的性能是增加。 他们也可能是其他原因。

只要它不公开, 一个类可以有一个不同于它的文件名的名称 。 class级也可以有主要的方法。 类文件将使用类名生成,但不会与源文件名一起生成。 类名应该用来执行它。

原因是:默认类是包私有的,因此javac不必从包的外部find这个源文件来编译一些其他的java程序。

晚上好,先生,Q)为什么public classname应该是java文件名? 答:

– >打开文件或读取文件操作系统或任何程序需要文件名。 – >开发者正在使用java语言。 开发人员正在给编译器指示创build.class文件,以便稍后执行。 – >要创build一个.class文件,java编译器必须打开并读取这个java文件。 为此,开发者通过两种方式给出指令1)直接指定文件名。 2)间接从其他程序————————————-第一种方式)(1)直接文件名。 ————————————-开发人员创build一个文件PrivateData.java并在这个文件内创build一个类PublicData PrivateData.java —————- class PublicData {private int x = 10; int getX(){return x; public static void main(String args []){System.out.println(“PublicData executed”); }} ———编译———- javac PrivateData.java

  DEVELOPER'S THOUGHT: now developer gives instruction to compilor to open and read PrivateData.java file and then create .class file for all the classes those are inside this file. COMPILOR BEHAVIOUR: in the above compilor behaviour is to read all the class declaration those are inside the PrivateData.java file and convert all those into .class file so, OUTPUT: PublicData.class file is created by compilor. ------------------------------------------- 2nd way) (2) indirectly from other program ------------------------------------------- let developer developed below tow java files (1)PrivateData.java (2)UsePrivateData.java PrivateData.java ---------------- class PublicData{ private int x=10; int getX(){ return x; } public static void main(String args[]){ System.out.println("PublicData executed"); } } UsePrivateData.java ------------------- class UsePrivateData{ public static void main(String args[]){ PrivateData pd=new PrivateData(); System.out.println(pd.getX()); } } --------- compile ---------- javac UsePrivateData.java DEVELOPER'S THOUGHT: now developer gives instruction to compilor to open and read UsePrivateData.java file and then create .class file for all the classes those are inside this file and indirectly gives instruction from one of the class/program, like in the above program is PrivateData pd=new PrivateData(); here developer give indirect instruction to compilor to create .class file by reading PrivateData.java (direct instruction is "javac PrivateData.java") COMPILOR BEHAVIOUR: ->in the above compilor behaviour is to read all the class declaration those are inside the UsePrivateData.java file and convert all those into .class file provided one of the classname is equal to the filename. ->now compilor got one more instruction from the program(indirectly from developer) instruction is PrivateData pd=new PrivateData(); now compilor behaviour is different. i) opens the file PrivateData.java ii)searches for the class PrivateData in the file PrivateData.java if found, create .class file for this class and then read all other classes and create .class file for those. if not found donot create .class file for other classes inside the file and raise compilation error like below ------------------------- UsePrivateData.java:3: error: cannot access PrivateData PrivateData pd=new PrivateData(); ^ bad source file: .\PrivateData.java file does not contain class PrivateData Please remove or make sure it appears in the correct subdirectory of the sourcepath. 1 error ---------------------- OUTPUT: depends on the above explanation. 

 WHAT I OBSERVED ------------------------------ ->for the indirect instruction compilor behaviour is to 1st search inside the file for any classname to be same as the filename. ->at a time computer/os/processor/compilor can execute one instruction. let one instruction is to open file and search the class whose name is equal to the filename to match this condition probably Sun Micorsystem/Oracle made a rule that only one public class can be declared inside file whose name should be equal to filename. (or) also , as compilor generates error if classname is not equal to the fileaname, so Sun Microsystem/Oracle made a rule for the good sake of developer that public classname should be equal to the classname and only one public class can reside inside a java file. 

我的答案是正确的,不是我不知道我只是试图给出正确的答案先生。