Why in java we have to write class name same as file name?? And without same name it shows that main function cannot load?

10/22/2019 5:13:20 PM

Gaurav Rawat

5 Answers

New Answer


Technically class name and file name must only be identical if the class is public accessible. The JVM looks for the filename in the file system to run it. If the (public) class name is different from the file name, the JVM cannot find it. If it wasn't like that JVM would need some kind of map/dictionary to link a class identifier to it's corresponding file to be able to call it. That would decrease performance.


The Java Virtual Machine, see https://en.m.wikipedia.org/wiki/Java_virtual_machine


It is not compulsory to give same name as class name. when we compiled the code then .class file is generated and jvm run that .class file instead of running our file. *so u can compile your file with any name but when u running your file then u need to write .class file name*


JVM what is ?


JVM intended to make things happen