How to use Packages In Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreekandank
    New Member
    • Jun 2009
    • 45

    How to use Packages In Java

    Introduction:
    Java allows you to group classes in collection called a package. Packages helps to separate your work from code libraries. The standard Java library contains a number of packages including java.lang, java.util, java.net, and so on.
    The main reason for using package is to avoid conflict between classes. For example, suppose two programmers come up with the same Employee class. Then both of them place their class into different packages. So there is no conflict between classes.

    Class Importation:
    A class can use all classes from its own package and all public classes from other packages. You can access the public classes in another package in two ways:
    1. By adding the full package name in front of every class name.
      For example,
      Code:
      class packageDemo
      {
       public static void main(String args[])
       {
        java.util.Date today=new java.util.Date();
        System.out.println("Todays Date : "+today);
       }
      }
    2. By placing import statements at the top of your source files.
      For example,
      Code:
      import java.util.*;
      class packageDemo1
      {
       public static void main(String args[])
       {
        Date today=new Date();
        System.out.println("Today: "+today);
       }
      }

    Static Imports:
    JDK5.0 or higher versions supports for importing static methods and fields from packages. For example,

    import static java.lang.Syste m.*;

    Then you can use static methods and fields of the System class without the class name prefix. That is:

    out.println("We lcome...");

    Defining a Package:
    To create a package, simply include a package command as the first statement in a Java source file. Any classes declared within that file belong to the specified package. When you create a package, the sub directory contains the package file must be sane as the package name. For example, the following statement creates a package called 'mypack'.

    package mypack;

    The java files that contains this mypack must be stored in a directory called 'mypack'.

    Example:

    Create the sub directory 'mypack' - contains the following three classes:
    1. Addition.java
      Code:
      package mypack;
      public class Addition
      {
       public double add(double a,double b)
       {
        return a+b;
       }
      }
    2. Multiplication. java
      Code:
      package mypack;
      public class Multiplication
      {
       public double mul(double a,double b)
       {
        return a*b;
       }
      }
    3. Division.java
      Code:
      package mypack;
      public class Division
      {
       public double div(double a,double b)
       {
        return a/b;
       }
      }


    Create another java file named as 'packageDemo.ja va' that make the use of 'mypack' and its classes.

    packageDemo.jav a
    Code:
    import mypack.*;
    /**
    *@author Sreekandan.K
    */
    class packageDemo
    {
     public static void main(String args[])
     {
      Addition a=new Addition();
      System.out.println("Addition:"+ a.add(5,3));
      Multiplication m=new Multiplication();
      System.out.println("Multiplication:"+m.mul(5,3));
      Division d=new Division();
      System.out.println("Division:"+ d.div(5,3));
     }
    }
    Note: For our convenient, place this(packageDem o.java) file into the bin directory of jdk1.3.1, that is:C:\jdk1.3.1\ bin. To make the execution successfully open the attached output window below.

    Author : SREEKANDAN.K
    Attached Files
    Last edited by MMcCarthy; Nov 6 '11, 08:45 AM. Reason: adding code tags
Working...