Myths about FileName Should be Same as Class Name in Java

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 className.

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.

Program Used 

class ClassA
	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 you can Compile Java Program where file name is 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 image given below to successful compilation and interpretAfter going through these all steps you can easily understand that we can save filename with different name. hence there is 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 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


About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in CodeProject, Java, Tips&Tricks and tagged . Bookmark the permalink.

20 Responses to Myths about FileName Should be Same as Class Name in Java

  1. Biswa Ranjan Mohapatra says:

    I do not understand the statement , “This is possible even the main() is public/private”. How could the main function be private which we are expecting to be the entry point of the class.
    Second thing is , the example you have given is a class with default scope, however if you define the class name as public , you have to give the source file name same as the class name .

    • Your Concern is very true that the entry point of your program should be visible and its also said that executing class should contain a ‘public static void method.. but it doesn’t mean that we can’t declare main method as private even we can declare, we can compile and of course we can run but in that case its can’t taken as entry point. we just invoked from somewhere.
      Lets come to your second concern… No need to give source file name as class name and same thing I tried to put in this article. Its a kind of myth to make file name as same as class name.

  2. sneha says:

    if you define the class name as public , you have to give the source file name same as the class name .?
    But then why cant we have a different name in case of public modifier ?

  3. jitendra rai says:

    Continuing the sehna question if we declare a class name with public specifer then we can’t name the file name other then the class name
    for example-:
    public class A
    public static void main(String arg[])

    // here we have to name the file name same as the class name other wise it give error try u self

    please clear our doubt

    • Hi Jitendra ! sorry bro for late reply..let me clear the concept, look we are free to use any name for file name only when the class is not public. In case of public class we are bounded to use file name as same as class name means we can not use different file name by anyway. Its a strong methodology of Java.

      Please feel free to revert back in case of any query.

      • jitendra says:

        sir ji that’s what i want to know the reason behind it .why we have to use the same name if the class is in public

      • Jitendra.. if really some one can answer to this question is Sun-Microsystems because they imposed this weird rule on us. It is just the convention set by the makers and the reason was stated that to find the source and class files needed at compile and runtime fast I don’t know that really it would compile and run slowly when we won’t follow this rule.
        Look some programmer says that this rule simplifies class loading. As being Java learner we better know that we can have only one public classes in one Java file so each public class must be in a file where the FileName matches the ClassName and a package where the Packagename represents the Directory structure. This make compiler able to find the source files and the class loader has to be able to find the implementation. Matching package names and classnames makes this really simple and, more important, fast.
        This rule is not needed in non-public class because non-public classes have a very limited visibility and can only be used within the package where they are defined. complier can easily trace the file when because of limited visibility.
        We all know Java always enforces good practices where they making sense but Java is often critized for putting restricting on the programmer in many ways like this one.

        Jitendra bro.. feel free to revert back in case of any query or discussion.

  4. sushmita nigam says:

    thanx sir awesome explanation

  5. Chiara Barcelona says:

    Interesting article. Thanks for sharing, but why almost every author of book give focus on using same class name as file name.

    • This is all because it enforces good practices where they make practical sense. it is not just a convention. It serves a purpose: to find the source and class files needed at compile and runtime fast.

  6. micheal says:

    great article

  7. waitingForU says:

    nice artical..thanks a lot

  8. shubham gupta says:

    if class name is public but does not contain main method,and an another class contain main method but not decleared public,then what will name of file name?

  9. heenaamb says:

    awsum man thanks a lot for the clarification like this

  10. thanks sir … for this article

  11. Vijaya Lakshmi says:

    Thanks a lot for clear explanation…

  12. Chandra Mani Gupta says:

    No doubt in this that we can have different file name and class name is our class is not declared as public. But when we compile our code, the bytecode (basically .class file) which is generated as the intermediate code has the name of the CLASSNAME rather than FILENAME. And whats the point that you are executing (using java-interpreter) the program on basis of generated CLASSNAME.We can never execute our program in this context with the given FILENAME in anyways.However, its really a bad practice, not industry standard (as you mentioned though) to use the different FILENAME and CLASSNAME (that contains main() method). Anyways nice article.

  13. Sriram E says:

    Wow…How a clear explanation…. Now I’m clear…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s