Java Source File Structure and Declaration rules
Package Declaration:
Package declaration is optional; if source file contains the package declaration then it comes first. There can be at most only one package declaration per source file. This package declaration is applicable to all the classes in the source file.
Import statements:
Import statements are optional; if source file contains import statement then import statements comes after the package declaration and before the class declaration. If there is no package declaration then import statements comes first in the source file. These import statements are applicable to all the classes in the source file.
Class Declaration
Class declaration comes after the package declaration and import statements.
- Java source file can contain only one public class
- Java source file can have more than one non public classes
- If there is a public class then name of the public class must match with the name of the class
Comments:
Comments and white spaces can appear before, after or in between the above declarations.
0 comments:
Post a Comment