When we define multiple classes in a source file and there are import statements and package statements, we must pay special attention to the following rules.
1. There can be only one public class in a source file, and there can be multiple non-public classes in a source file;
2. The name of the source file should be the same as the class name of the public class. Keep it consistent;
(Video tutorial recommendation: java video)
For example: the class name of the public class in the source file is People, then the source file should be named People. java.
3. If a class is defined in a package, the package statement should be on the first line of the source file;
4. If the source file contains an import statement, it should be placed in the package statement. and class definition. If there is no package statement, the import statement should be in the first line of the source file;
5. The import statement and package statement are valid for all classes defined in the source file;
6. In the same source file, different package declarations cannot be given to different classes.
Recommended tutorial: Getting started with java development
The above is the detailed content of Introduction to source file declaration rules in java. For more information, please follow other related articles on the PHP Chinese website!