0

why are filenames same as public class in java any idea?

In java why we use public ,private and string

22nd Jan 2021, 8:09 AM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
4 Answers
+ 2
This question and answer should answer you: https://stackoverflow.com/questions/2134784/why-are-filenames-in-java-the-same-as-the-public-class-name Here's the start in case that link eventually breaks: 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...
22nd Jan 2021, 9:16 AM
Josh Greig
Josh Greig - avatar
+ 1
It is a choice of Java's language designers and I didn't design Java but I can list a few benefits: - the files containing a public class name are easier to find for both developers, compilers, and java virtual machine. - it is consistent with how Java packages line up with the file system. A full class name includes its package. For example java.lang.String is the complete class name for String. If you had source for that, it would go in the directory structure java / lang / String.java. The relationship between that file path and the complete class name is very easy to understand. Having java / lang / SomethingElse.java hold the class definition for String would be more confusing. You'd also have to deal with multiple files potentially redefining the same class.
22nd Jan 2021, 9:31 AM
Josh Greig
Josh Greig - avatar
0
I have seen but didn't understand 😔
22nd Jan 2021, 9:18 AM
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎
𝗣𝗮𝘄𝗮𝗻 𝗞𝘂𝗺𝗮𝗿 🅟︎🅚︎ - avatar
0
Public modifies visibility of a class Private modifies visibility of a field String is a datatype just like numbers and letters are datatypes in real life.
22nd Jan 2021, 10:32 AM
D_Stark
D_Stark - avatar