The file name can be anything but the extension must be java.
Within one source file, any number of custom type(s) i.e. class, interface, enum, and annotation can be defined.
A file name can be anything if there isn't any public custom type (class, interface, enum, annotation).
If there is a public custom type then the source file name must be the same as the public custom type name.
Within one source file, there can be only one public custom type. If required, any number of non-public types can be defined.
If there are multiple custom types within a source file then for every custom type separate class file will be generated.
All the classes defined in a source code file can have the main method.
At the time of execution, the class name must be specified with the java interpreter. If the specified class has the main method then it will be executed otherwise an error message will be displayed.
At the time of compilation, the compiler will check for syntax error(s) in the source file. If there is any syntax error then none of the class file(s) will be generated.
If no syntax error is found then the compiler will verify grammatical rules for the individual types from the first custom type. If there is no grammatical error then only the class will be generated and the next class will be verified.
The following command can be used to verify the task of the compiler:
javac –verbose <file name>.java
In a source file, a package statement must be the first statement, if any.
Import statements must come after package statement (if available) and before the first custom type is declared.
There can any number of import statement(s) in a source file.
Package and import statement(s) will be used for all the types declared in the source file.