Why is it required for a java file to have a same-named class inside
of it? What if there is no class with such name inside of it?
Because java compiler needs to start the execution from the public
class inside the .java
file . You can have any number of class inside , but only one public class with the name of the program
If a file contains several classes with main() method inside of them,
which one will be selected when launching the file? And again, what if
there's no such method in any class of the file?
Try it yourselves , you can overload the main()
in a class. It will start execution from default . Have a look at Multiple main() methods in java and also Use of class definitions inside a method in Java to get some clear understandig