Friday, 14 October 2011

Defining Package


The  package command is included as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The general form of the package statement:

 package package _name;
The keyword package is used to define a package . A Package_Name is the name of package that will contain the classes and interfaces defined in the file.
Ex, package p1;
Package can be nested within other packages. For this , in the package package statement, separate each package name with a dot(.).It creates a package hierarchy. The general form of multileveled package statement is shown here:

package pkg1[.pkg2[.pkg3]];

 A package hierarchy must be reflected in the file system of your Java development system. For example  a package is declared as .

package java.awt.image;

Adding Class to Package

 The package p1 contains one public class named A. To add a class  to package p1 consider following.

· Define the class and make it public.

·         Place the package statement.
package p1;
       Before the class definitions as follow
     package p1;
     public class A;
       {
    // body of A
       }

·         save file as A.java file under directory p1
·         Compile A.java file this will create a A.class file and place it in the directory p1.
More than one file can include the same package statement. The package statement simply specifies to which package the classes defined in a file belong. It does not exclude other classes in other files from being part of that same package.

Ex
                                                                                             
package p1;
       class amount
   {
String name;
double amt;
amount(string n,double a)
{
name=n;
amt=a;
 }
 void show()
   {
if (amt<0)
            {
println(“No amount”);
                                                                            }  
println(name +” “ +amt);
         }
         class pack
           {
       public static void main(string args[])
        {
     amount a1[]=new amount[2];
     a1[0]=new amount(“Radha”,500);
     a1[1]=new amount(“Krishna”,600);
     for(int i=0;i<2;i++)
     {
  a[i].show();
     }
                 }
               }


Here, the package p1 has class named amount with member variables and methods  and the file name  is pack.java.Put it in a directory called p1 . Next, compile the file make sure that the resulting class file is also in the p1 directory. Then try executing the pack class, using the following command line.

 java p1.pack

The class pack is now part of the package P1. The means that it cannot be executed by itself. That is you cannot use this command line:

java pack

pack must be qualified with its package name.

Naming convention (Rules) for using  the packages
  1. Package names are written in all lowercase to avoid conflict with the names of classes or interfaces. 
  2. The directory name must be same as the name of package that is created using "package" keyword in the source file.
  3. Before running a program, the class path must be picked up till the main directory (or package) that is used in the program.
  4. If not include any package in  java source file then the source file automatically goes to the default package.
  5. In general, we start a package name begins with the order from top to bottom level.
  6. In case of the internet domain  , the name of the domain is treated in reverse (prefix) order. 

Import the Package
Packages are a good mechanism for compartmentalized diverse classes from each other. All of the built in Java classes are stored in packages. Since classes within packages must be fully qualified with their package name or names it could become tedious to type in the long dot separated package path name for every class you want to use. For this reason, java includes the import statement to bring certain classes or entire packages into visibility.

Once imported, a class can be referred to directly using only its name.

In java source file import statements occur immediately following the package statement and before any class definitions.

Syntax :
                                                                                    import pkg1[.pkg2].(classname |*);

Here, pkg1 is the name of a top level package and pkg2 is the name of a Sub ordinate package inside the outer package separated by a dot(.). There is a no limit on the depth of a package hierarchy. Except that imposed by the file system. The classname specify that it is the special class or a * indicates that the java compile should import the entire package.

All of the standard Java classes includes with java are stored in a package called Java. The basic language functions are stored in a package inside of java package called Java.lang java. Long is implicitly imported by the compiler for all programs. 

Ex, 
Create file named Arith.java . Save its class file in package pack1

package pack1;

public class Arith
{
    double n1,n2;

    public void getData(double x1, double x2)
    {
        n1 = x1;
        n2 = x2;
    }

    public void show()
     {
         System.out.println("Number 1 : "+n1);
         System.out.println("Number 2 :  "+n2);
     }

  public double add()
   {
    return(n1+n2);
   }
}

Now create another file with name ArithPack as:

import pack1.Arith;

class ArithPack
{
  public static void main(String args[])
   {
           
       Arith ob1=new Arith();
       ob1.getData(20,30);
       ob1.show();
       double ans=ob1.add();
       System.out.println("Answer of addition = "+ans);

   }
         }

As shown in above example in file ArithPack it  import  the Arith class of pack1 package and can access the methods of Arith class. Same as one class , more than one class can also be imported with separate import statement.

Posted by : Ruchita Pandya

No comments:

Post a Comment