Packages In Java

In general, Package is an item or group of items wrapped in a box. We generally use this terminology to describe Parcels. It is part of our daily life, for example, if we order anything from online store then item which we have ordered is termed as a package and similarly, we receive the notification like Your Package will be arriving in the next two days.

If you have ordered multiple items then those items can be delivered to you in a single or multiple packages, depending on your order type and shipping.

Java Packages also work on the same mechanism to encapsulate(enclose in) a group of classes, sub packages and interfaces in a single or multiple Package depending on your requirement.

We have two types of Java Packages: built-in packages (standard packages which come as a part of Java Runtime Environment) and the packages we can create (also known as a user-defined package).

Packages are used for:

  • Preventing naming conflicts.
  • Making searching/locating and usage of classes, interfaces, enumerations, and annotations easier.
  • Providing controlled access.
  • Packages can be considered as data encapsulation (or data-hiding).
  • In packages, classes can be declared uniquely compared with classes in other packages.
  • Packages also allow programmers to separate design from coding.

Built-in Packages :

These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are shown in the table below :

Package Name Description
java.lang  Contains language support classes ( for e.g classes which defines primitive data types, math operations, etc.). This package is automatically imported.
java.io  Contains classes for supporting input/output operations.
java.applet  Contains classes for creating Applets.
java.awt  Contains classes for implementing the components of graphical user interface (   like buttons, menus, etc. ).
java.net  Contains classes for supporting networking operations.
java.util  Contains utility classes which implement data structures like Linked List, HashTable, Dictionary, etc and support for Date / Time operations.

User-defined packages :

These are the java packages that are defined by the user. First, we create a directory then create the class inside the directory with the first statement being the package names.

Illustration of user-defined packages:

  • Creating our first java package:

  • Creating our second package:

  • Making use of both the packages created above:

Rules for Package :

  • Class stored within a package is called “packaged class”
  • Package class should start with: “package packagename;
  • To access packaged class outside the package, it should be declared as public.
  • A packaged class needs to compiled and executed from outside the package.

Compile :

Interpret :

Note: Classes from the same package need not be imported.

  • Specific import:  This import overrides other types of import.

  • In case of ambiguity inline import is used :

  • General import:

  • To call classes from other drives/directories, CLASSPATH needs to be set:

NOTE: only the classes that are stored within package can be imported

Example


Find the working example of Package implementation of codedbug GitHub repo.

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?