Why are the file names in Java the same as the name of the public class?

In Java, the file name must match the name of the public class contained in this file. Why is this a limitation? What purpose does he fulfill?

+54
java
Jan 25 '10 at 19:01
source share
9 answers

Java had an interesting approach: when giving a programmer a choice can only degrade the quality of programming, remove that choice.

They did it in several places. File names 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 it, work with!), Etc.

I am very sorry that they did not go any further. There is no reason for public variables: I never needed it, and I never saw a situation where some clever programmer thought he needed it and was right.

I also would not want to see restrictions on the size of the method / class, but this could become sketchy (it could be easily implemented using code checks, usually the problem is that the companies that need the most help are those who do not know that they need help and therefore do not use tools such as code checks).

This is not important for most small teams, but when your team grows and has several sites with consultants from India, China and other other places around the world, you will begin to appreciate the inflexibility.




In response to the setter / getter message:

Java beans were an abomination created by Borland to crack their GUI, and then were translated into Java.

A terrible idea - distraction from OO programming - Getters and seters A) show too much of your implementation and B) make you think about manipulating the data of another object, rather than asking another object to perform an operation for you. Bad hacking for people who still can't think in OO.

Sometimes it is necessary that getters are necessary, but they cannot be added unless they are considered absolutely inevitable.

Seniors should be avoided at all costs. If you absolutely need an external state change after creating the object, try using the builder pattern and protect your setters from being called after any operation.

Obviously, there are exceptions in everything, and many "Getters" are actually critical object business logic, such as String.length (), which would be required no matter how String was implemented, and not even implemented just by returning the property - great case for "getter" if you even want to call it.

+43
Jan 25 '10 at 19:14
source share

I was going to say that this is just a must . But I looked at JLS and it is not so strict. From a JLS perspective, the compiler has to choose whether to set such a restriction or not.

Almost verbally - general compilers have this limitation, and, as already mentioned, it is much easier for the compiler to find the compiler or for the class loader to find the class file with such a restriction in place.

+17
Jan 25 '10 at 19:12
source share

To be more specific, the file name should have the same name as the public class name in this file, which is a way to tell the JVM that this is what is your entry point.

+10
Jan 25 '10 at 19:06
source share

This is just an agreement established by Sun, the creators of Java.
The goal is organization; the reason is that anyone who types code in Java will have a consistent way of naming files.

+5
Jan 25 '10 at 19:05
source share

Each open class should be in a file where FileName corresponds to ClassName and the package, where Packagename represents the Directory structure, written in dotted form (slashes become dots, such as com / example / app becomes com.example. Application).

This agreement is not accidental. The compiler should be able to find the source files, and the class loader should be able to find the implementation. Matching package names and class names makes this very simple and, more importantly, fast.

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

+2
Jan 25 '10 at 19:14
source share

Q. "Then how does it work in the case of C ++, where there is no such restriction?"

but. This does not work. You must have a make file. You do not need it in Java.

+2
Feb 15 2018-10-15T00
source share

This is useful when looking for a class. Suppose that different file names are allowed, and if you created an instance of the class, then the compiler should look for the class in all files, and if the file names are the same as the class, then the performance of searching and using the class has increased. There may be other reasons.

+1
Jul 20 '11 at 13:29
source share

While it is not publicly available , the class may have a name other than the file name . A class may also have a main method. The class file will be generated with the class name, but not with the name of the source file. The class name must be used to execute it.

Reason: default class is a closed package, so javac should not look for this source file to compile any other Java program from outside the package.

-one
Feb 07 '14 at 10:09
source share

Good evening, sir, Q) Why should the public classname be the name of the java file? Answer:

-> to open a file or read a file. The operating system or any program requires a file name. -> the developer uses the java language. the developer instructs the compiler to create the .class file so that it can be executed later. -> to create a .class file, the java compiler must open and read the java file. for this reason, develope gives instructions in two ways: 1) directly the file name. 2) indirectly from another program ------------------------------------- 1st path) (1 ) directly the file name. ------------------------------------- the developer creates the PrivateData.java file and the PublicData class is created inside this file PrivateData.java ---------------- class PublicData {private int x = 10; int getX () {return x; } public static void main (String args []) {System.out.println ("PublicData done"); }} --------- compile ---------- javac PrivateData.java

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

MY ANSWER IS CORRECT OR I DO NOT KNOW I PLEASE TO PROVIDE THE CORRECT ANSWER sir.

-3
Jul 14 '13 at 10:09
source share



All Articles