Turn Desktop View Off
by Dinesh Thakur
  1. The programmer can easily find them, by knowing their position with their set of task. The common types of classes and interfaces are placed in the same package.
  2. The type name (class or interface) must not conflict with other package type name, because at the same time if both packages are in use, compiler must not confuse.
  3. There must not be any restriction to access any type from another type within the same package. But assign the restricted access from types outside the package.

Defining a Package

Now as we know, a package is a collection of classes and interfaces. A package does not contain the source code of any type of Java file. Each Java source code file is a collection of one or more classes, or one or more interface with their members. After compilation, a separate class file is created for individual class or interface, either these are part of one Java file or stored in individual file. That class file (byte code) is placed in a particular related package using a package declaration.

Declaration Syntax:

                   package <fully qualified package name>;

 

The package declaration statement must be the first statement of the source file, and at least one package declaration must be in a source file. The package name is stored in the byte code (class file). The dot (.) notation is used to distinctively spot (identify) the package name in the package hierarchy. One naming convention for a package is that, package name is written in all lowercase. It will escape the confliction with classes and interfaces name.

 

Example:

 

package mypkg;

 

Here, mypkg is the package name. Moreover a file belonging to same package can include the same package name.

You can also create a hierarchy of packages, i.e. package within a package called subpackage for further classification and categorization of classes and interfaces.

 

                   package mypkg.mypkg2.mypkg3;

 

Here, mypkg2 is a subpackage in package mypkg, mypkg3 is a subpackage in subpackage mypkg2. If there is no package declaration in a source file, then the compiled file is stored in the default package (unnamed package), and that is the current working directory.

                             Package Hierarchy

In the above package hierarchy class A is identified by the name pckl.pck2.pck3.A .This is called the fully qualified type name of the class. (type). A fully qualified type name (name includes class or interface name) is comprises offully qualified package name. According to above hierarchy pck1.pck2.pck3 is the fully qualified package name .

 

Packaging up Classes

 

Now, you must have a model or a blue print of package in your mind. Let's take one example how to create package with keeping in mind all the concepts that we have discussed in last sections.

 

Example:

 

package pck1;

class Student

{

                           private int rollno;

                           private String name;

                           private String address;

                           public Student(int rno, String sname, String sadd)

                  {

                               rollno = rno;

                               name = sname;

                               address = sadd;

                   }

                            public void showDetails()

                   {

                            System.out.println("Roll No :: " + rollno);

                            System.out.println("Name :: " + name);

                            System.out.println("Address :: " + address);

                    }

}

                    public class DemoPackage

          {

                           public static void main(String ar[])

                 {

                            Student st[]=new Student[2];

                            st[0] = new Student (1001,"Alice", "New York");

                            st[1] = new Student(1002,"BOb","Washington");

                            st[0].showDetails();

                            st[1].showDetails();

                   }

            }

 

There are two ways to create package directory as follows:

 

1. Create the folder or directory at your choice location with the same name as package name. After compilation of copy .class (byte code file) file into this folder.

 

2. Compile the file with following syntax.

 

javac -d <target location of package> sourceFile.java

 

The above syntax will create the package given in the sourceFile at the <target location of pacakge> if it is not yet created. If package already exist then only the .class (byte code file) will be stored to the package given in sourceFile.

 

Steps to compile the given example code:

 

  1. Compile the code with the command on the command prompt.

       javac -d DemoPackage.java

  1. The command will create the package at the current location with the name pck1, and contains the file DemoPackage.class and Student.class
  2. To run write the command given below

       java pckl.DemoPackage

 

Note: The DemoPackate.class is now stored in pck1 package. So that we've to use fully qualified type name to run or access it.

 

Output:

 

Roll No :: 1001

Name :: Alice

Address :: New York

Roll No :: 1002

Name :: Bob

Address :: Washington

 

Note: If you put multiple types in a single source file, only one can be public, and it must have the same name as the source file. For example, you can define public class Circle in the file Circle.java, define public interface Draggable in the file Draggable.java, define public enum Day in the file Day.java, and so forth.

 

You can include non-public types in the same file as a public type (this is strongly discouraged, unless the non-public types are small and closely related to the public type), but only the public type will be accessible from outside of the package. All the top-level, non-public types will be package private .