You are on page 1of 3

Package :> A Package is directory. > Package is a collection of classes and interfaces. > Package canbe reused by anyone.

> We can also create the our own packages. > Packages can be used with 'import' keyword. > Descrition of contents of the package is available in API Document(Jus t like Help file). > API Document is a HTML Document which has description of a package. > Before using the Package, then that package mustbe present in the path of classpath. Creating the Sample Package: before going to write the package, we must create a directory named as 'pool', it contains package source code. c:\bbp>md pool c:\bbp>cd pool c:\bbp\pool>notepad Addition.java (Here Addition.java is a package program) Addition.java: ************ //package demo package pack; //here it will create a package directory named as 'pack' public class Addition { double x,y; public Addition(double x,double y) { this.x=x; this.y=y; } public double add() { return x+y; } }//end class (now compile the this program) c:\bbp\pool>javac -d . Addition.java the above command indicates, it creates the sub directory(-d) named with 'pack' in the current directory(.) then after the compilation of java program, the class file is created in pack di rectory. letus our 'pack' package is move to 'd:\work' directory... Before going to using that pack package, it will must present in classpath. Defination of classpath: classpath is variable, it is a active path, when ever we place the packa ges in that path, then that packages are available to rest of computer. classpath can be set in two ways:

1.Using the command: set classpath=d:\work; To show the current classpath give the command as c:\bbp\pool>echo %classpath% d:\work; 2.Set classpath in environment variables: Going to My Computer properties, press on Advanced Tab then goto Environment Var iables. In that there are two variables they are User and System Variables. In User variables click on 'New' button, then it will display the small window, then give the path name as 'classpath' and value as 'd:\work'. then apply and ok. Now YOu can use that package any where in system. Letus write a java program to import the pack package. c:\bbp\pool>cd.. c:\bbp>UsePack.java UsePack.java *********** //use the user package import pack.*; class UsePack { public static void main(String args[]) { Addition obj=new Addition(10,20.2); System.out.println("Sum = "+obj.add()); } }//end class c:\bbp>javac UsePack.java c:\bbp>java UsePack Sum = 30.2 NOTE: When compile this UsePack.java program in that directory pack.java program must not present in same dirctory because when compile this program JVM first searches in same directory with that package name without extension. If that package program is available then JVM opens that file, then byte code is no t available in that. Then it gives the comilation error as 'remove the package program from that directory'. So that why package s ource code and other programs must be present in different directories.

You might also like