Packages in Java

tutorial and example
tutorial and example

Packages in java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalogue for holding various java files. They provide a very efficient mechanism for alignment of various files. The files which are related to each other can be stored in the same package, and whenever there is a need to access the related files, we just have to go through one package. A unique namespace is provided by the package.

A package can contain following types of file in java:

  • Class
  • Interface
  • Enumerated types
  • Annotations

Advantages of packages

  • Two classes in two different packages can have the same name so, name collision issue is not a problem.
  • Classes related to each other are stored in the same package so, accessing them is easy.
  • Access protection is provided by packages so, no need to worry about the security concerns. The files that are crucial can be hidden successfully.
  • Programmer can make their own packages as per their convenience. It is programmer friendly.
  • Code reusability is provided.

Types of packages in java

Built-in packages: They are the ones which are predefined by the java compiler. They can be imported to use by the user.

User-defined packages: They are the ones which are made by the programmer to group related classes, enumerations, annotations and interfaces as required by him.

Built-in packages in java.

There are some built in packages in java that can be used as it is by the programmer. They are listed below.

Let us take a look at each one of these packages:

  1. java.lang: This package contains the language support classes. They are by default imported in the java programs.
  2. java.util: This package contains the language utility classes such as vectors, lists, hash tables, etc.
  3. java.awt: This package contains a set of classes for implementing the graphic user interface.
  4. java.applet: This package contains a set of classes for using applets in java program.
  5. java.net: This package contains a set of network classes.
  6. java.io: This package contains classes for input and output support.

User defined packages

Having seen the predefined packages, now it’s time to see how the packages can be created, imported and used by the users.

Creating Packages

  • Select a suitable name for the package.
  • Declare the name of package with the ‘package’ keyword in the first line of program. E.g. package firstpack.
  • Define a public class inside that package.
  • Create a subdirectory in the main directory to store the class. Save it as classname.java.
  • Compile the file.

E.g.

Importing Packages

After the creation of packages, it can be imported using the ‘import’ keyword. Packages can be imported in two ways.

  • By explicitly declaring the class name to be imported: E.g. import firstpackage.secondpackge.classname;
  • By importing all the classes in a package. E.g. import packagename.*;

The drawback of using the shortcut approach is that, it is difficult to determine from which class a particular member came. While using the shortcut approach, all the classes get imported. If the programmer wishes to hide some of the class then it is advised to use access modifier other then public for that class.

Using Packages

After having seen how packages are created and imported, let us have a look at how they are used. In the program given below we will see all the steps mentioned.

pack1 is the user defined package and ‘Calculator’ is the name of the class. The program is saved with the name Calculator.java.

Once defined, let’s see how the package is used.

Program to import two packages simultaneously.

Now, we will make another class which will import both the packages.

This class uses both the packages simultaneously. We can import as many packages as we desire.

How to add a new class to an existing package?

There is an example given below to add a new class to an existing package.

This is the package in which we wish to add a new class, it can be done as:

  • Define the class and use public modifier.
  • Place the package statement at the top of the program.
  • Compile the class as done earlier.
  • The class is now added to package.