A JAVA program file can only contain one public java class and this should be named the same as the java file itself.

There can be any number of classes with / without other modifiers.

Classes without any modifiers will get compiled even if the name of the class does not match the name of the java program file.

JAVA compiler will look for the name of the .java files associated with the call to a class (during object instantiation).

Some JAVA compilers will warn the users even when the non-public class names do not match the file names.

When you indicate that the class file is “public” it explicitly states that the program will be referenced elsewhere. Compiler checks if the name of the class matches the name of the file at that instance and will throw an error message to indicate the inconsistency.

Rule of thumb: Save the java files in the name of the primary class referenced in the program file.