So I was reading following article:
https://dzone.com/articles/why-single-java-source-file-can-not-have-more-than
On the topic of why we cant have more than one public class in one file and there was this statement:
And also as soon as we execute our application, JVM by default looks for the public class (since there are no restrictions and it can be accessible from anywhere), and it also looks for public static void main(String args[]) in that public class. The public class acts as the initial class from where the JVM instance for the Java application (program) is begun. So when we provide more than one public class in a program the compiler itself stops you by throwing an error. This is because later we can’t confuse the JVM as to which class is to be its initial class, because only one public class with the public static void main(String args[]) is the initial class for JVM.
This confuses me because of following reasons:
- When we compile the code compiler would create seperate .class files for every class
- When we execute the code we are telling JVM in which class the main method is example
java Test
(where Test is an .class file) which means that jvm only has to look if the main method is defined inside this .class file
So what does the statement means that JVM looks for main method inside public classes?