Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for:
- Preventing naming conflicts. For example there can be two classes with name Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee
- Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
- Providing controlled access: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only.
- Packages can be considered as data encapsulation (or data-hiding).
How did they manage to include two classes with the same name Date in JDK?
This was possible because these two Date
classes belong to two different packages:
java.util.Date
– this is a normal Date class that can be used anywhere.java.sql.Date
– this is a SQL Date used for the SQL query and such.
Based on whether the package is defined by the user or not, packages are divided into two categories:
Built-in Package
Built-in packages are existing java packages that come along with the JDK.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 | mport java.util.ArrayList; class ArrayListUtilization { public static void main(String[] args) { ArrayList<Integer> myList = new ArrayList<>(3); myList.add(3); myList.add(2); myList.add(1); System.out.println(myList); } } |
Output:
1 | myList = [3, 2, 1] |
User-defined Package
Java also allows you to create packages as per your need. These packages are called user-defined packages.
How to define a Java package?
To define a package in Java, you use the keyword package
.
1 2 3 | └── com └── test └── Test.java |
1 | package com.test; |
Here, any class that is declared within the test directory belongs to the com.test package.
1 2 3 4 5 6 7 | package com.test; class Test { public static void main(String[] args){ System.out.println("Hello World!"); } } |
Output:
1 | Hello World! |
Package Naming convention
The package name must be unique (like a domain name). Hence, there’s a convention to create a package as a domain name, but in reverse order. For example, com.company.name:
1 2 3 | └── com └── company └── name |
How to create a package in Intellij IDEA?
In IntelliJ IDEA, here’s how you can create a package:
- Right-click on the source folder.
- Go to new and then package.
3. A pop-up box will appear where you can enter the package name.
Once the package is created, a similar folder structure will be created on your file system as well. Now, you can create classes, interfaces, and so on inside the package.
How to import packages in Java?
Java has an import
statement that allows you to import an entire package (as in earlier examples), or use only certain classes and interfaces defined in the package.
The general form of import
statement is:
1 2 | import java.util.Date; // imports only Date class import java.io.*; // imports everything inside java.io package |