The package statement is used to declare packages. The word package is a keyword in Java. This statement is not technically needed to write a complete Java program. However, if it appears, it must be the first executable statement in the program. Only comments or white spaces are allowed before a package statement. There can be only one package declaration in a program. If no package declaration appears, the class will belong to the default package.
For example, the following source code file defines a class named ClassA in the package named package4:
package package4;
public class ClassA{
{
// body code
}
This source file will be saved with the name ClassA.java in the directory named package4.
Responses to “How to declare a package?”