While writing source code, following are the rules of declaring package, import statement and class:
- There can be only one
publicclass per source code file. It means, in one .java file, there can not be two or morepublicclass file. There should only be onepublicclass in a source code file. - You can declare multiple non
publicclass in a single source code file. - If there is a
publicclass in a source code file, then the name of the file should be same asclassname. For example:
public class Foo {
//your code
}
This class should be saved as Foo.java as the the source code has class which is public.
- Comments can appear at the beginning or the end of file.
- If the class is a part of package, then
packagestatement should be the first statement of the file.packagestatement should come before anyimportstatement. importstatement should come afterpackagestatement and before class declaration. If there is nopackagestatement thenimportstatement should be the first statement in the source code.- If there are multiple non
publicclasses in a source code file, the source code file can be saved as a different name that does not match with any of the class in the file.
