package keyword in java
The "package"
keyword specifies the Java package in which the classes declared in a Java source file reside.
Example
- package com.mycompany.services;
- public class MyCompanyService {
- }
package com.mycompany.services; public class MyCompanyService { }
In the above example, the source file MyCompanyService.java must be placed under the package /com/mycompany/services
Rules regarding the package statement
A source file can be placed under a specific package or default package.
The package
statement (if specified) must be the first line in the source file.
Package name must follow the pattern: level1.leve2.level3….leveln
where,
level1.leve2.level3….leveln are the sub packages in the same order.
If a Java source file does not contain a package statement, the classes defined in the file are in the default package.
Note that classes in the default package can not be referenced from classes in non−default packages.
In the example above, the fully−qualified class name of the MyCompanyService class is com.mycompany.services.MyCompanyService