Here is the complete structure of a Java source file discussed in previous



package java application;


import classes;


public class NewClass


         public NewClass() {



The package statement allows us to group logically related classes into a single unit. In our earlier programs, we have used import statements such as the following:


import java.util.*;

Both and java.util in these statements are packages. All I/O-related classes are grouped together and put under the package Similarly, all the utility classes are grouped together under the java.util package. The Java Development Kit defines many such packages, grouping the various classes in different logical units depending on their functionality.

When we develop several classes in our application or when we create several applications, we may also want to arrange our classes into logical units. We do so by creating packages in our application. To create a package, we need to use a package statement. The basic syntax of the package statement is shown here:

package packagename;

A package name may consist of zero or more subpackage names, each separated by a dot.

Here are a few valid package declarations:
  • package mypackage;
  • package mypackage.reports;
  •  package mypackage.reports.accounts.salary 

1.    The first statement declares a top-level package called mypackage.

2.    The second statement declares a subpackage called reports within mypackage.

3.    The third statement declares a package hierarchy where mypackage is the top-level package, with the reports package within it, the accounts package within reports, and finally the salary package within accounts.

Rules for Package Decelerations

When you declare a package, the declaration must follow certain rules:

·       The package must be declared at the beginning of the source file before any other statement.

·       Only one package declaration per source file is permitted.

·        Package names must be hierarchical and separated by dots.

·       If no package is declared, the compiler creates a default package and all classes that do not have a package declaration will be grouped under this default package.

As the first rule states, a package declaration must be a top-level statement in our source file. Writing a package declaration statement anywhere else in the source program results in a compile-time error.

As the second rule states, we cannot have more than one package declaration in a single source file. Note that a source file may contain multiple class declarations; however, it can contain only one package declaration. All classes defined in the source file will be grouped together in a package declared at the top of the source file.

As the third rule states, if we decide to use subpackages, the entire name must be hierarchical and the subnames must be separated by a dot. Imagine the hierarchical package name as a directory structure containing a main directory, followed by a subdirectory in each, until the end of the full package name. A class belonging to the package will be put under this last subdirectory following the entire directory structure. The directory layout for a source containing a package statement is described in the next section.

Finally, the last rule states that if the source file does not contain a package declaration, all classes defined in the source file will be grouped together in a default package. The directory for this default package is the current directory itself. Thus, all classes defined in this source file will be put in the current folder when compiled.

  Modified On Mar-14-2018 06:38:19 AM

Leave Comment