CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@micheal john • 07 Jan, 2014
In Java Why Java class name and file name should be same?

i googgled and found some blogs saying
1) class name and file name should be same only when the class is public

but could not understand core reason for this approach?

😖
@Anoop Kumar • 07 Jan, 2014 A public class is meant to accessed by anyone and for the record a class file is byte-code format not in plain-text.
Now how can you access a public class if NameOfTheFile and NameOfTheClass is not same?

Edit: rephrasing , it will make you difficult organize your code.
@micheal john • 07 Jan, 2014
Anoop Kumar
A public class is meant to accessed by anyone and for the record a class file is byte-code format not in plain-text.
Now how can you access a public class if NameOfTheFile and NameOfTheClass is not same?

yes i agreee,

but my question is "class name and file name should be same only when the class is public?" is it true
@Anoop Kumar • 07 Jan, 2014 Yes,
Otherwise you already know class name where to access. In that case no need to have same name.
@Onkar Pathak • 08 Jan, 2014 In java, the class which contains public static void main that name and file name should be same.. because all other classes are accessed through main class and by giving file name same as class containing main you are refering to main.. hence it needs to be same....
@Anoop Kumar • 08 Jan, 2014 So the class with modifier public which do not have public static void main method
can have different file name?
@micheal john • 08 Jan, 2014
Anoop Kumar
So the class with modifier public which do not have public static void main method
can have different file name?
even i have the same doubt?
@Anand Tamariya • 08 Jan, 2014 That's part of JLS:
If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
  • The type is referred to by code in other compilation units of the package in which the type is declared.
  • The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.
@Anoop Kumar • 08 Jan, 2014 There is no relation between public class/filename and public static void main. main method is just starting pointer of a java class (if class have this method with valid signature).

Adding few more points to @Anand Tamariya @JDKDocs
-To ease javaDoc -
The -javadoc command processes a source file only when the file fulfills all of the following requirements:
The file name prefix (with .java removed) is a valid class name.
-To avoid multiple public classes in one file

This restriction makes it easy for a Java compiler to find a named class within a package. 👍
@micheal john • 09 Jan, 2014 Thanks guys now i understood why java public class name should be same as file name😀
@infoj • 05 Jul, 2015 why even that restriction to have the same name as class name at run time.
Answer is that is how java interpreter will know which class to load and where is the entry point (main() method) otherwise interpreter may have to scan a lot of class files to determine where to start.
@Kunal Deshpande • 09 Jul, 2015 As long as you don't have a public class in your source file, you can name your source file to any name and can compile. But, if you have a public class in your source file, that file should have the name same as your class name. Otherwise, compiler will throw an error.
@msajaa • 10 Jul, 2015 Answer is simple,because to make sure there is no confusion while compiling and running the program.
@msajce • 10 Jul, 2015 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.
@Gnanam Ramalingam • 24 Dec, 2015 The compiler will generate a .class file with the name of the class at the time of executing we need to specify the name of the class to be executed i think this can be a one of the reason to name the source code file with its class name...............this is what content i have refered
@Gnanam Ramalingam • 24 Dec, 2015 name of the file should be named after the class which holds the main function
@rahul69 • 10 Jan, 2016 Logical answer: Java was made that way, C++ wasn't 😉.
@Mohana Chandra • 26 Jan, 2020

It is not mandatory to use File name and class name to be same. But for user convenience we maintain it to be same

Command for Compilation: javac Sourcefilename. java

Command for Execution : java Bytecodefilename

6.2k views

Related Posts

@Ankita Katdare · Feb 6, 2015

Superbike lovers have a big fat reason to rejoice as Suzuki Motorcycle India has announced the arrival of its nothing-short-of legendary superbike, the 'Suzuki Hayabusa Z' in a limited edition...
10.2k views

@Ankita Katdare · Jul 22, 2014

Micromax has launched a brand new 32 inch LED TV in India as an addition to its portfolio which has two more LED TVs to offer. The Micromax 32T42ECHD is...
9.4k views

@Satya Swaroop Dash · Jul 23, 2014

What do you get if you downgrade the specs of Nokia Lumia 630 and target it for the budget audience? You get the new Nokia Lumia 530. The first offering...
7k views

@mibawad · Sep 12, 2011

Hello, I want to use a DC permanent motor as a generator in order to use it to charge a battery. This generator will be rotated by hand. The speed...
5.8k views

@EDDY · Jul 24, 2009

Whats the difference between CNC and NC machine and do u people know any institute which teaches CNC programming
45.8k views