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:
- By adding the full package name in front of every class name.
For example,
Expand|Select|Wrap|Line Numbers- class packageDemo
- {
- public static void main(String args[])
- {
- java.util.Date today=new java.util.Date();
- System.out.println("Todays Date : "+today);
- }
- }
- By placing import statements at the top of your source files.
For example,
Expand|Select|Wrap|Line Numbers- import java.util.*;
- class packageDemo1
- {
- public static void main(String args[])
- {
- Date today=new Date();
- System.out.println("Today: "+today);
- }
- }
JDK5.0 or higher versions supports for importing static methods and fields from packages. For example,
import static java.lang.System.*;
Then you can use static methods and fields of the System class without the class name prefix. That is:
out.println("Welcome...");
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:
- Addition.java
Expand|Select|Wrap|Line Numbers- package mypack;
- public class Addition
- {
- public double add(double a,double b)
- {
- return a+b;
- }
- }
- Multiplication.java
Expand|Select|Wrap|Line Numbers- package mypack;
- public class Multiplication
- {
- public double mul(double a,double b)
- {
- return a*b;
- }
- }
- Division.java
Expand|Select|Wrap|Line Numbers- package mypack;
- public class Division
- {
- public double div(double a,double b)
- {
- return a/b;
- }
- }
Create another java file named as 'packageDemo.java' that make the use of 'mypack' and its classes.
packageDemo.java
Expand|Select|Wrap|Line Numbers
- 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));
- }
- }
Author : SREEKANDAN.K