«Back to Home

Core Java

Topics

Package In Java

Package
 
In Java, package is a collection of similar types of classes, interfaces and sub-packages. Package is used to avoid name conflicts and control the access of a class, interface and enumeration etc. Using package, it can be converted easily to locate the related classes.
 
Packages are two types in Java, which are,
  • Built-in package

    Built-in package is predefined in Java.

    For example- java, lang, awt, javax, swing, net, io, util, sql etc.

  • User-defined package

    User-defined package is created by the user.

    For Example- mypack, pack etc.
Why we use package in Java
  1. Packages are used to classify the classes and interfaces, so that they can be easily maintained.

  2. Packages give an access protection.

  3. Package eliminates the naming collision.

    42
Let’s see an example of package, given below.
 
In Java, package keyword is used to create a package. Creating package in Java is very easy. First, include a package command, followed by the name of the package as the first statement in Java source file.

Code
  1. package Brands;  
  2. public class Puma {  
  3.     public static void main(String args[]) {  
  4.         System.out.println("Welcome to our Brand");  
  5.     }  
  6. }  
43
Output

44

In the example, mentioned above, we create a package, whose name is Brands and one simple class Puma.
 
If we are not using any IDE, how can we compile Java package?
 
Syntax

javac –d directory javafilename
 
Let’s see an example.
 
javac –d . Puma.java
 
The -d switch identify is the destination, where to place the generated class file. We can use any directory name like a:/xyz (in case of windows) etc. If we want to keep the package within the same directory, we can use . (dot).
 
Now, how to run Java package program
 
We need to use fully qualify the name of program.
 
Example
 
Compile: javac –d . Puma.java
 
Run: java brands.Puma
 
The -d is a switch, which tells the compiler, where to locate the class file i.e. it represents the destination. The . represents the current folder.
 
Access package from another package
 
Import Keyword

In Java, import keyword is used to import built-in and the user-defined packages into our Java source file. Thus, our class can refer to a class, which is in another package by directly using its name.
 
Three approaches to access the package from outside the package are,
  1. import package.*;

  2. import package.classname;

  3. Using fully qualified name.
Import all the classes from the particular package. (import package.*)

The import keyword is used to put together the classes and interfaces of this package will be accessible.
 
When we use package.* then all the classes and interfaces of this package will be accessible.
 
For example.
 
Code
  1. package brands;  
  2. public class Puma {  
  3.     public void display() {  
  4.         System.out.println("Welcome to our Brand");  
  5.     }  
  6. }  
  1. package mybrand;  
  2. import brands.*;  
  3. public class Levis {  
  4.     public static void main(String args[]) {  
  5.         Puma b = new Puma();  
  6.         b.display();  
  7.     }  
  8. }  
45

46

Output

47

Import the only class you want to use. (package.classname)

When we import package.classname, only declared class of this package will be accessible.
 
For example
 
Code
  1. package brands;  
  2. public class Puma {  
  3.     public void display() {  
  4.         System.out.println("Welcome to our Brand");  
  5.     }  
  6. }  
  1. package mybrand;  
  2. import brands.Puma;  
  3. public class Levis {  
  4.     public static void main(String args[]) {  
  5.         Puma b = new Puma();  
  6.         b.display();  
  7.     }  
  8. }  
48

49

Output

50
 
Using fully qualified name

When we use fully qualified name, only declared class of this package will be accessible. Now, there is no need to import the package but we need to use fully qualified name each or every time, when we want to access the class or an interface but this is not a good practice. It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class.
 
For example
 
Code
  1. package brands;  
  2. public class Puma {  
  3.     public void display() {  
  4.         System.out.println("Welcome to our Brand");  
  5.     }  
  6. }  
  1. package mybrand;  
  2. public class Levis {  
  3.     public static void main(String args[]) {  
  4.         brands.Puma b = new brands.Puma(); //using fully qualified name    
  5.         b.display();  
  6.     }  
  7. }  
51

52

Output

53

Import statement must be writing after the package statement.

For example
  1. package mybrand;  
  2. import java.lang.*;  
If we are not creating any package in our program and import statement will be the first statement of our Java program.
 
If we import a package, subpackages will not be imported.

When we import a package, all the classes and interface of that package will be imported but not including the classes and interfaces of the subpackages. Hence, if we want to use any subpackage that time, we need to import the subpackage as well.
 
Let’s see the sequence of the program, which is,
 
54
 
When we declare one package inside another package, it is called the sub-package in Java. It should be created to categorize the package further.
 
Summary

Thus, we learnt that package is a collection of similar types of classes, interfaces, sub-packages and also learnt how to use it in Java.