Why in java we have to write class name same as file name?? And without same name it shows that main function cannot load? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

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

22nd Oct 2019, 5:13 PM
Gaurav Rawat
Gaurav Rawat - avatar
5 Answers
+ 8
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.
22nd Oct 2019, 6:08 PM
Tashi N
Tashi N - avatar
24th Oct 2019, 8:06 PM
Tashi N
Tashi N - avatar
+ 1
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*
24th Oct 2019, 10:27 AM
pankaj sharma
pankaj sharma - avatar
+ 1
JVM what is ?
24th Oct 2019, 8:05 PM
rokia atia
+ 1
JVM intended to make things happen
25th Oct 2019, 12:24 PM
Михаил Сергеевич Давыдов
Михаил Сергеевич Давыдов - avatar