Java Tutorial 21 – Package
| A package is a collection of related classes.It helps Organize your classes into a folder structure and make it easy to locate and use them.
More importantly,It helps improve re-usability. Syntax:- package; |
The following video takes you through the steps of creating a package.
Please be patient . Video will load in some time. If you still face issue viewing video click here
Assignment 1:To create a package
Step 1) Copy the following code into an editor
package p1;
class c1{
public void m1(){
System.out.println("Method m1 of Class c1");
}
public static void main(String args[]){
c1 obj = new c1();
obj.m1();
}
}
Step 2) Save the file as Demo.java. Compile the file as, javac – d . Demo.java
Step 3) Run the code as java p1.c1
Assignment 2) To create a sub-package
Step1) Copy the following code into an editor
package p1.p2;
class c2{
public void m2(){
System.out.println("Method m2 of Class c2");
}
public static void main(String args[]){
c2 obj = new c2();
obj.m2();
}
}
Step 2) Save the file as Demo2.java. Compile the file as javac – d . Demo2.java
Step 3) Run the code as java p1.p2.c2
importing a package
To create an object of a class (bundled in a package), in your code, you have to use its fully qualified name.
Ex.
java.awt.event.actionListner object = new java.awt.event.actionListner();
But , it could become tedious to type in the long dot-separated package path name for every class you want to use. Instead it is recommended you use the import statement.
Syntax
import ;
Once imported , you can use the class without mentioning its fully qualified name.
Ex:
import java.awt.event.*; // * signifies all classes in this package import javax.swing.JFrame // here only the JFrame class is imported //Usage JFrame f = new JFrame; // without fully qualified name.
Assignment 3: To import package
Step 1) Copy the code into an editor.
// Using packages created in earlier assignment
package p3;
import p1.*; //imports classes only in package p1 and NOT in the sub-package p2
class c3{
public void m3(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
c1 obj1 = new c1();
obj1.m1();
p1.p2.c2 obj2 = new p1.p2.c2();
obj2.m2();
}
}
Step 2) Save the file as Demo2.java . Compile the file using the command javac –d .Demo2.java
Step3) Execute the code using the command java p3.c3
Packages – points to note:
- To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex : com.guru99. com.microsoft, com.infosys etc.
- When a package name is not specified , a class is into the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
- While creating a package, care should be taken that the statement for creating package must be written before any other import statements
// not allowed import package p1.*; package p3;
//correct syntax package p3; import package p1.*;
the java.lang package is imported by default for any class that you create in Java. You can study it more in detail here.
The java API is very extensive , contains classes which can perform almost all your programming tasks right from Data Structure Manipulation til Networking. More often than not , you will be using API files in your code. You can see the API documentation here.
