Package Program in Java
The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages. Package programs in Java are the best examples of data hiding (or data encapsulation). There are two types of packages in Java namely pre-defined/ Build-in packages and user-defined packages.
Built-in packages are those packages that are comes with JDK. We have to include it in our code and use it. Some of the pre-defined packages are:
Sub-packages are those packages that reside inside a package. We have to import a sub-package explicitly as the automatic import feature does not come with a sub-package.
Let us observe the following code for a better understanding of the above concepts.
Filename: PackageExample.java
Output:
Explanation: As we have imported all classes of the java.util package, therefore, we have created objects of Vector and LinkedList class. However, the util package is not only confined to these two classes. It contains a lot of other classes, too, which we have imported and not used in our code. That is why this type of import is generally avoided. We must import only those classes which we are going to use in our code. Thus, the first line of code can be replaced by the following:
Here, we are only loading Vector and LinkedList class, which a good practice. Please note that the util is a package that resides inside the java package. Hence, we can say the util is a sub-package. Since java.lang package is already imported. Therefore, the statement System.out.println(); always works.
We can also re-write the above program to generate the same output.
Filename: PackageExample1.java
Output:
Explanation: Since we have not used the import statements. Therefore, we have to provide every package detail along-with the class name.
The Static Import
Static import is the unique feature applicable for Java 5 (or above). This type of import allows the methods and fields present in a class as public static to be used in the Java code. The following example demonstrates the same.
Filename: PackageExample2.java
Output:
Explanation: Since out is a static and public member of the System class, and we have already done a static import on the System class. Therefore, we can omit the word System in the code.
The packages that are defined by the user are known as user-defined packages. The first step is to create a directory of any name of one’s choice. In our case, we will use the name firstPackage. Inside the firstPackage directory, we create a .java file called BasicPrograms.java.
Filename: BasicPrograms.java
// The package name must match the name of the directory.
Outside the firstPackage directory, we create another .java file, namely PackageExample3.java.
Filename: PackageExample3.java
Now, compile the PackageExample3.java file using javac PackageExample3.java and execute it using java PackageExample3. We will get the following output.
Output:
Explanation: The visibility of the class BasicPrograms will be maximized as we are using public access specifier. The same goes for the method printNames(). Therefore, we can easily create an object of BasicPrograms and invoke the method printNames(). But for that to work, we must import BasicNames class, which is achieved with the help of the firstPackage.
source