Click here to Skip to main content
12,446,746 members (57,128 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

Myths about FileName Should be Same as Class Name in Java

, 9 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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 … Continue reading →

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 interpret

    After 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. :smile:

For More info  you should navigate


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer

An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.

He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..

He keep himself on the desk of his imagination, hanging around with some inceptions.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 9 Jan 2014
Article Copyright 2014 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid