This is very important session for us to know about myth of File name in Java. When I started to learn Java I got that every book, resources and even most of web blogs stated that we should keep the “name of Java file as the name of class.” I wonder why we should do it? Why every one asking to follow the weird rule? Why can’t we save java source file as other 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 question “why file name in java should be same as class name.”
Note: We are allowed to use any name for file name only when class is not public. In case of public class we can’t use different file name. The filename must have 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. Its not necessary to keep filename as class name but it is strongly recommended that we should keep filename as
What is Class
In object oriented programming class plays a vary important role without a single class we can not imagine of successful execution of program. class is a collection of object where object may be Method or variable or both. For Compilation of program a main Method [Entry Point ] must be exist in 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
Note: In this program class name is
classA and file name is
How to compile it
If you are a Java programmer then you already know that how to compile a program but for 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 exist
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 same number of .class file as classes resides 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 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 any one. 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 headache 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