This is a very important session for us to know about the myth of File name in Java. When I started to learn Java I got that every book, resources and even most of the web blogs stated that we should keep the “name of Java file as the name of the class.” I wonder why we should do it? Why is everyone asking to follow the weird rule? Why can’t we save java source file as another name (which is not matching with class name)?
Why everywhere it is written that FileName should be same as className. So my friends at the end of this article or better to say after getting outlines of this article we would know the answer of the question “why file name in java should be same as class name.”
Note: We are allowed to use any name for filename only when the class is not public. In the case of public class, we can’t use the different file name.
The filename must have the same name as the public class name in that file, which is the way to tell the JVM(Java Virtual Machine) that this is what the entry point is for you.
Interesting point:- You can keep any name for java file name. It’s not necessary to keep filename as class name but it is strongly recommended that we should keep filename as className.
What is Class
In object-oriented programming class plays a very important role without a single class we cannot imagine of successful execution of the program. the class is a collection of an object where the object may be Method or variable or both.
For Compilation of program, the main Method [Entry Point ] must exist in the class. Without entry point, we can’t imagine the compilation of code.
public static void main(String args)
This program is written in java language and save as ClassName.java
Note:- In this program class name is classA and file name is ClassName.java
How to compile it:-
if you are a Java programmer then you already know that how to compile a program but for a beginner who does not know how to compile a java program, take a look and follow the steps as given below:-
- press Windkey+r [it will open run option]
- write cmd and press Enter [it will open Command Prompt]
- now navigate the path as given below [note in this article program path is set to F:\Java]
- Type CD command and set the directory where java source code File exists
- Now you can Compile Java Program where the filename is ClassName.java and javac is a java compiler.
- After compiling it gives a class known as ClassA.class [because ClassA is name of Class in Program].
- After getting class you can interpret this class file with java interpreter which is java comes from java.exe
- To interpret – type java and classfile which is “classA”.
as:- java ClassA and Press Enter
- Take a look of the image given below to successful compilation and interpretAfter going through these all steps you can easily understand that we can save filename with the different name. hence there are no rules to save Filename always with ClassName. It is not mandatory to say “file name equals to classname”. you can give your own name to your filename [ other than classname ] . at the time of compilation you just give your filename[other than classname]. After compilation, you will get .class file with your class name.[classname.class] . But at the time of loading your program into JVM(Java Virtual Machine) you just have to give the class name. This is possible even with main() is public or private.
Now let’s see,
Why it’s recommended that fileName should be same as className
Suppose when you create a program in which more than one class resides. And after compiling Java Source File, it will generate the same number of the .class file as classes reside in your program. In this condition, you will not able to easily identify which class need to interpret by java interpreter and which class containing Entry point (Main Method) for the program. You can take a look at Source code for class Names and then you have to find out classname in which Main Method residing. So it will be very tedious work for anyone. Therefore we are forced to follow the rules to makes finding other classes slightly easier for the compiler in the beginning since it doesn’t have to search all the potentially thousands of class files for a random public class, it can just go to the file.
Best practice – Having multiple public classes in the same file makes things confusing. The purpose of files is to organize the source code, the purpose of folders is to organize the files. If all the classes of a particular package are in a single 100 MB super file then you’ve lost all the advantages and gained none of the benefits of files (plus adding lots of headaches when editing)
It is just the convention set by Sun, the makers of Java and the purpose is organization of programs therefore to find the source and class files needed at compile and runtime becomes fast; the reason is so that everyone who codes in Java will have a consistent way of naming files.
So finally We say this statement that the file name should be same as the class name to make sure there is no confusion while compiling and running the program. 🙂
For more info, you should navigate