Package in Java is a mechanism to encapsulate a group of related classes, sub packages and interfaces together.
These classes will all be related in some way, e.g.:
- belonging to the same category
e.g., they might all have to do with a specific application;
- having similar functionality,
e.g., they perform a specific set of tasks.
A package can contain the following types:
- annotation types.
Packages help to:
- Avoid naming conflicts
A Java package organizes Java classes into namespaces,
providing a unique namespace for each type it contains.
We can define two classes with the same name in different packages to avoid name collision.
E.g., there can be two classes with name Employee in two packages,
college.staff.cse.Employee and college.staff.ee.Employee
- Write a better maintainable code
Define reusable code in form of classes inside a package.
After that we can simply import a class from existing packages and use it in our program.
We can reuse existing classes from the packages as many times as we need it in our program.
- Better Organization makes it easier to search for/locate and use classes, interfaces, enumerations, and annotations.
For several hundreds of classes, it is always required to group the similar types of classes in a meaningful package name so that you can organize your project better and when you need something you can quickly locate it and use it, which improves the efficiency.
- Controlled access:
public, protected and default have package level access control.
Public members and classes are visible everywhere
and private members are visible only in the same class.
Classes in the same package can access each other’s package-private and protected members.
A protected member is accessible by
classes in the same package
and its subclasses
(classes in other packages that are subclasses of the declaring class).
A default member (package-private) is accessible by
classes in the same package only.
- Packages can be considered as data encapsulation (or data-hiding).
- Java packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually.
Package naming conventions
The Java Language Specification establishes package naming conventions
to avoid the possibility of two published packages having the same name.
The naming conventions describe how to create unique package names,
so that packages that are widely distributed will have unique namespaces.
This allows packages to be separately, easily and automatically installed and catalogued.
Packages are usually defined using a hierarchical naming pattern,
levels in the hierarchy are separated by periods (., pronounced “dot”).
Although packages lower in the naming hierarchy
are often referred to as “subpackages”
of the corresponding packages higher in the hierarchy,
there is almost no semantic relationship between packages.
→ java is a top level package
→ util is a sub package
→ Scanner is a class which is present in the sub package util.
Packages are named in reverse order of domain names,
(highest hierarchy on the left):
- top level domain name of the organization
- organization’s domain
- any subdomains
- organization can then choose a specific name for its package
- subsequent components of the package name
(vary according to an organization’s own internal naming conventions)
You can visualize a package as a folder/sub-folder structure in a file directory.
E.g, if a package name is college.staff.cse,
then there are three directories, college, staff and cse;
cse is located in staff and staff is located in college
(think of it as college/staff/cse in terms of folders in a file directory).
(think of it as org/tarima/yuwebdesign in terms of folders in a file directory).
The directory college is accessible through CLASSPATH variable,
i.e., path of parent directory of college is present in CLASSPATH.
The idea is to make sure that classes are easy to locate.
Types of Java packages
Packages are divided into two categories:
- Built-in Packages (packages from the Java API)
- User-defined Packages (create your own packages)