In java programming, packages is a collection of related classes and each class define number of method.
The classes define inside the package that
Java packages are classified into two types:-
- Java API(Application Programming Interface) packages or Predefined packages or Built-in packages: These packages are defined by the system. Some example of system defined packages
are java.lang, java.util, java.io,java .awt , java.applet etc. The Java 1.3 contains nearly 60 predefined User-defined packages: These packages are defined by the user.
Defining as packages: To define a package, place “package” command as the first statement in the source file.
The syntax of packages creation :-
1 | package package_name; |
1 2 3 4 5 6 7 | mypackage; public class number { public void add(int a, int b) { System.out.println(" a + b =" + (a + b)); } } |
- The class that is defined in the package must be public. So that is can be accessible by any another of them.
- Java uses the file system file directories to store packages. We save the program with number.java and compile the package is as javac_d/number.java. Due to this compilation Mypackage directory is automatically created and .class file is stores in that directory.
Once the package creation has completed. Then the package information is including with the help of “import” keyword. import keyword links the package with our program.
It
1 2 3 | import mypack.*; or import mypack.number; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package x; // package x public class Number // file name must be saver with class name(number.java) { public void add(int a, int b) { System.out.println(" sum =" + (a + b)); } } package y; import x.number; public class Test { public static void main(String a[]) { number ob = new number(); ob.add(10, 20); } } |
1 | sum = 30 |

- In this program, package is within x folder and main program is within y folder in c drive.
- Package name in this program is x.
- File name and package class name must be same ex: (number.java- source file).
How to compile:
1 2 3 4 | javac directory/filename c:\javac x/number.java c:\javac y/xyz.java c:\java y/xyz |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package math1; // package within math1 folder public class Cal // file name must be saver with class name(Cal.java) { public void square(int a) { System.out.println(" Square =" + (a * a)); } public void cube(int a) { System.out.println(" Cube =" + (a * a * a)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package x; import math1.Cal; public class Test { public static void main(String a[]) { Cal ob = new Cal(); ob.square(10); ob.cube(10); } } |
1 2 | square = 100 cube = 1000 |
Note: If we want to access all the classes of information then the classes are stored in different files with the same package name.
Sub Packages
In Java, it is possible to create a sub packages for the main packages.
Syntax
package pack1.pack2;
1 | package pack1.pack2; |
Where pack1 is the main package and pack2 is the sub package.
1 2 3 4 5 6 7 8 9 | package pack1; public class x { public void show() { System.out.println(" super"); } } |
1 2 3 4 5 6 7 | package pack1.pack2; public class y { public void display() { System.out.println("sub"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package z; import pack1.x; import pack1.pack2.y; public class Test { public static void main(String a[]) { x ob1 = new x(); ob1.show(); y ob2 = new y(); ob2.display(); } } |
Result
1 2 | super sub |