Why Are Filenames in Java the Same as the Public Class Name

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

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.

Languages make a lot of decisions like this, they all affect usability and maintainability. Some others that might have been good choices:

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.

Method/class size limitations would be great, 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).

The point of the answer is that although isn't stuff that matters to most small teams, when your team grows and has multiple sites with split teams and consultants throughout the world, You really 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.

Class name same as filename in java?

public classes have to be in a file with the correct filename. Non-public classes can be in any file you want. Even multiple classes in the same file if it is convenient.

what is happening when a class with access modifier public when a java file with file name and class name different

If you define a class as public, then class name should match the file name.

The reason we define any class as public is to make it accessible from other classes or code in other packages.

This rule of naming file name after its class name makes it easy for the compiler to find the class.

Also in JAVA, we can have only one public class in a file.

Can I compile a java file with a different name than the class?

Your Java file name should always reflect the public class defined within that file. Otherwise, you will get a compiler error. For example, test.java:

public class Foo {}

Trying to compile this gives:

[steven@scstop:~]% javac test.java
test.java:1: class Foo is public, should be declared in a file named Foo.java
public class Foo {
^
1 error

So you must have your filename match your public class name, which seems to render your question moot. Either that or I don't understand what you're asking... spending some time explaining what you are actually trying to achieve would go a long way towards asking a more effective question :)



Related Topics



Leave a reply



Submit