Package Program in Java
In Java, a package is a group of classes, sub-packages, and interfaces. It supports organisation of existing classes into a folder structure, making them easier to find and utilise. More importantly, it promotes the reuse of code. Each package has its own name. The classes and interfaces are organised into a separate namespace, or name group.
Interfaces and classes with the same name can't be found in the same package, although they can be found in distinct ones. It is accomplished by giving each Java package its own namespace.
Create Package in Java
Creating a package is a straightforward procedure as explained below:
Choose the package's name.
In Java Source File, include the package command as the first line of code.
The source file includes the classes, interfaces, and other components of the package that we want to include.
To generate Java packages, you must first compile them.
Step 1: Consider the Java package application below:
To place a class into a package, specify package p1 on the first line of code.
Make a C1 class.
Creating a method named m1 that prints a statement.
Identifying the primary method.
Create an object of the class C1.
Method m1 is being called.
Step 2: Save the file as a demo.java in the next step.
Step 3: We compile the file in the new step.
The compilation process is now complete. The C1 class file is generated. But why isn't a package created? The solution is in the next step.
Step 4: Now we must create a package by using the following command.
The compiler is forced to construct a package using this command.
The current working directory is represented by the "." operator.
Step 5: Run the above code, it produces a package named p1 when we run it. The C1.class file may be found when we open the java package p1 within.
Step 6: Use the following code to compile the same file.
The parent directory is indicated by "..". In our example, the file will be stored in the C Drive's parent directory.
When the preceding code is run, the file is stored in the parent directory.
Step 7: Suppose, we want to establish a sub package p2 within our existing java package p1. Then we will make the necessary changes to our code.
Step 8: Prepare the file for compilation.
It generates a sub-package p2 with the class C1 inside the package p1.
Step 9: To run the code, type the fully qualified name of the class, which is the package name followed by the sub-package name, then the class name.
As explained in above steps the package is executed, and the output is "m1 of c1" from the code file.
Importing a Package
In order to construct an object of a class (bundled in a package), we must use its fully qualified name in your code.
However, typing the lengthy dot-separated package path name for each class we want to use might get laborious. It is advised that we use the import statement instead.
Once imported, we can use the class without mentioning its fully qualified name.
Example: To import package
Step 1: Copy the code into an editor.
Step 2: Save the file as Demo2.java. Compile the file using the command javac -d . Demo2.java
Step 3: Execute the code using the command java p3.c3
Packages - important notes:
To avoid naming difficulties, packages are given names that are reversed versions of the company's domain name. For example, com.microsoft, com.infosys,
When no package name is supplied, a class is placed in the default package (the current working directory), that has no name. As a result, we were able to complete assignments earlier.
When writing a package, it's important to remember that the statement for constructing the package must come before any other import statements.
The java.lang package is imported by default for any class that we create in Java.
The Java API is quite comprehensive, with classes that can handle practically any programming activity we can think of, from data structure manipulation to networking. We will almost certainly use API files in our code.