Learn this and get that part of the certification correct (if that question were to appear).
My guess is that there should only be one 'entry point' into a class file, easier for the compiler. Say that more than one public class is present in a file and the command java classname is issued; which class file should be used? The filename doesn't represent the callable class anymore, so how to relate the class to run to the file in which the class is specified? If you were to solve this, you have to change the 'java' command (and the compiler) and make some effort to specify which of the 'main' methods to activate. By setting the specification as SUN has done, you keep things much simpler.
Dolphins are grey, but they dream in colour.
I recently learned here that there is a historical reason why this is so. Long ago, when Java wasn't yet called Java (it was called Oak at that time) this was necessary for the compiler to be able to import packages efficiently.
Why is each public class in a separate file? (Section 1)
This is a question that I have frequently been asked during my courses. Up to now I have not had a good answer to this question. In section 1, we read: "Although each Oak compilation unit can contain multiple classes or interfaces, at most one class or interface per compilation unit can be public".
In the sidebar it explains why: "This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"
It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units (.java files) to figure out what classes were where, and that would make the compilation even slower.
Originally posted by Mohit Jain: This is to tell the JVM which class contains the main() method and where to begin the execution from.
No, this requirement is for all classes, not just for applications. It is kind of an arbitrary requirement, and there used to be compilers that didn't enforce it (I think the Metrowerks IDE didn't care about it).
Ping & DNS - updated with new look and Ping home screen widget