Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

like image 415
Thunderhashy Avatar asked Jan 25 '10 19:01

Thunderhashy


People also ask

Why is Java public class name and filename same?

The main reason for the class and file name to be same are to make the job of the complier easy to check which class it needs to run, in the whole list of the Java classes. So it's a good practice to have filename and class name as same.

Why should the public class name match the file name in Java?

The filename must have the same name as the public class name in that file, which is the way to tell the JVM that this is an entry point. Suppose when we create a program in which more than one class resides and after compiling a java source file, it will generate the same number of the .

Why can't we have more than one public class in the same file?

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.

Can a class have the same name as its field?

Yes you can do it. It's called obscuring because if the variable is in scope it becomes impossible to refer to the class.


2 Answers

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

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:

Java beans were an abomination created by Borland to hack their GUI up, then retrofitted into 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.

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.

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.

like image 82
Bill K Avatar answered Oct 12 '22 08:10

Bill K


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.

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.

like image 35
Andreas Dolk Avatar answered Oct 12 '22 06:10

Andreas Dolk