+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?

10/22/2019 5:13:20 PM

Gaurav Rawat

5 Answers

New Answer

+7

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.

+5

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

+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*

+1

JVM what is ?

+1

JVM intended to make things happen