Packages in Java are generally referred to as a related group of classes, interfaces, sub-packages, enumerations, and annotations. Just like the literal meaning of ‘package’, JAVA packages tend to hold or bind together groups of classes that might be needed to perform a certain task.
The main purpose of creating packages in JAVA was to avoid name clashes and conflicts. Each package has its scope and normally does not encroach on the scope of other packages. There are certain exceptions to this but the packages fundamentally behave in this manner. This, therefore, allows two classes with the same name to co-exist in two different packages without raising naming conflicts. For example: school.history.student and school.geography.student are two classes with the same name ‘Student’ but in different directories (i.e. history and geography).
Why are Packages Used in Java?
With the inception of packages, it has become a lot easier to identify and locate classes. With the help of just one import statement at the beginning of your code, you can effectively use all the classes that the package has to offer in your program. The package is also a threshold for controlled access practices. It acts as a barrier for protected and default scopes. While a protected member can be accessed by all classes and subclasses of the package it is in, default members can only be accessed by classes of the current package. Data Encapsulation is another phenomenon that packages deploy. It hides the complex code from users and allows them to use classes of a package readily by the import statement.
What are the types of packages?
Packages can be broadly classified into two types: Built-In packages and User Defined Packages
- Built-In Packages – These packages are in-built in JAVA and have been coded by the developers of the Java API. One needs to use one package or the other for any small code or project. Some of the built-in packages that are indispensable are mentioned below:
lang – This provides language support to your code and is required when you use primitive data types. This package is automatically invoked when you start a new project.
java.io – This package deals with input and output streams.
java.util – This is the utility package of java and contains data structures like lists and arrays. It also provides essential classes for date/time operations.
java.applet – Used in Applet production
java.awt – This package specifically deals with the JAVA GUI (Graphical User Interface). It is one of the first imports that need to be made when you are developing a GUI application.
java.net – Dictates terms for network-related problems.
- User Defined Packages – Suggestive from the name, these are packages that the user creates and uses in a project. The process to build a package is simple. Firstly, you need to build a myPackage directory (name should be the same as the package same). Add a class to the package with a method in it and you are done. Import the package using the import statement followed by the package name and the class name separated by a dot.
Example: import SamplePackage.SampleClass;
How Packages in Java Work and how to Import them in Classes?
Packages have a simple naming convention. They are made of directories in a hierarchical order and separated by dots ‘.’.
For Example: university.department.engg.student
The following is a complete package name where university, department, engg, and students are all directory names and follow the same hierarchy.
To import packages in your codes, the following examples will help:
Both are valid statements. In the first import, the entire util package is invoked whereas, in the second import, only the Date class is invoked. This pattern is followed by all other packages. Packages can also be imported using the CLASSPATH variable.