Packages in Java

code Java

After blocks and methods in the previous post, here we will learn about a very useful feature i.e Packages in Java. We will also see how to create our own packages as well as get a brief idea about the predefined packages in Java.

A package is a group of similar types of classes, interfaces, and subclasses.

Types of Packages in Java:

Broadly there are 2 categories of packages.

  1. built-in packages
  2. user-defined packages

Packages provide access protection of its constituent classes. It becomes easy to maintain the classes by categorizing them through packages. Using packages can also avoid the Naming collision.

  1. Built-in packages:

    • The built-in packages are already provided by the Sun Microsystems company along with the JDK (Java Development Kit).
    • They contain numerous predefined classes which in turn contain numerous methods and functionalities.
    • These methods and functionalities help a programmer to easily use them just by importing the packages and calling the required methods.

 

Packages in Java

In the above picture, java is the default package. The sub packages of Java package are lang, util, and awt. The 3rd layer denotes the classes that are present inside the sub packages.

Here is a short description of the fundamental Java packages.

java.lang: This package provides methods and classes for achieving the language functionalities such as the conversion of data from strings, displaying results on the console. This package is by default imported for each and every program.

java.io.*: This package contains the functionalities for file handling applications such as opening files, reading and writing data into files.

java.awt.*: awt stands for Abstract Windowing Toolkit. This package helps to build GUI (Graphic Unit Interface) components such as buttons, checkboxes, forms, etc.

One package name is separated from another through a dot. For example, java.lang, java.swing, java.applet, java.io, java.awt etc.

 

  1. User-defined Packages

    • These are the packages that a programmer creates according to the need of programming.
    • It simplifies the task of the programmer by enclosing a most frequently used set of classes, interfaces and sub packages within a package.
    • The first sentence in the program must be the package creation statement.
    • You can create a package by using the word “package”.
package mypackage;

//the above statement creates a package

public class PackageCreation {

  public static void main(String[] args) {
    
    System.out.println("the package name for this program is mypackage");
  }
}

 

Accessing a package from another package:

Now that we have learned how to create a package, we need to see how to use the functionalities of one package inside another package. There are 3 ways to do this.

  • import package_name.*
  • import package_name.ClassName.*
  • Fully qualified names

Look at the below programs to see how you can import packages inside another package. The programs explain all the 3 ways of importing packages with the help of 2 packages i.e. DisplayMessage and test. The method display() inside the package DisplayMessage is used by a method inside the other package test.

By “import package_name.*”

package DisplayMessage;

public class DisplayMessageClass {
  
  //this class is inside package DisplayMessage
  
  public void display()
  {
    System.out.println("Welcome to Java Packages !! ");
  }
}

package test;

import DisplayMessage.*;

/*package DisplayMessage is imported. 
** .* implies all the classes present inside the package arex imported
*/

public class ClassA {
  public static void main(String[] args) {
    
    DisplayMessageClass obj = new DisplayMessageClass();
    
    obj.display();	
    
  }
  
}

Output:

By "import package_name.*"

If you write import package.*; then all the classes and methods will be accessible to the other class. But sub packages will not be available. The names of sub packages have to be written explicitly like import packageName.subpackageName.*;

By “import package_name.ClassName.*”

package DisplayMessage;

public class DisplayMessageClass {
  
  //this class is inside package DisplayMessage
  
  public void display()
  {
    System.out.println("Welcome to Java Packages !! ");
  }
}

package test;

import DisplayMessage.DisplayMessageClass;

//package DisplayMessage is imported with DisplayMessageClass

public class ClassB {

public static void main(String[] args) {
    
    DisplayMessageClass obj = new DisplayMessageClass();
    
    obj.display();	
    
  }
  
}

Output:

By writing Fully qualified names

If you do not import the package and want to use fully qualified name method, then you should explicitly write the fully qualified name each and every time you use any method or class of that package. Therefore this practice is not advisable as it may lead to rework and confusions.

package DisplayMessage;

public class DisplayMessageClass {
  
  //this class is inside package DisplayMessage
  
  public void display()
  {
    System.out.println("Welcome to Java Packages !! ");
  }
}

package test;

public class ClassC {

public static void main(String[] args) {
    
    //fully qualified name of class is used to create the object
    DisplayMessage.DisplayMessageClass obj = new DisplayMessage.DisplayMessageClass();
    
    obj.display();	
    
  }
  
}

Output:

To sum up the article, you must import packages before using their methods and classes. The package creation statement has to be the first statement of the program followed by the import statements.

In my next article, I will discuss in details Objects and Classes. Though we have been using classes and objects from the beginning of this tutorial, please go through my next article to know about the detailed characteristics and behavior of Objects and Classes.