Creating and using Packages in JAVA
Creating and using Packages in JAVA
In Java, packages serve as a system for organizing classes, sub-packages, and interfaces. They offer a way to group together classes with similar functionalities.
Once classes are organized within packages, they can be easily imported and utilized in our code. Packages act as containers for organizing related classes, with some classes accessible for external use while others are reserved for internal purposes.
The benefit lies in the ability to repeatedly utilize existing classes from packages as needed within our programs. Furthermore, package names are closely linked to the directory structure in the project.
Ways: There are two types of packages in java
Custom Packages (Creating Your Own Packages)
Built-in packages refer to those packages included in the Java Application Programming Interface (API), such as swing, util, net, io, AWT, lang, javax, and others
In this article, we will explore the process of creating a package in Java.
A package is a collection of related classes, interfaces, and potentially sub-packages. Packages are employed to prevent naming conflicts and to organize code efficiently.
Syntax: To import a package
import package.name.*;
Example: To import a package
// Importing the java utility package
import java.util.*;
// Main Class
public class Main {
// Main driver method
public static void main(String[] args) {
// Create a Scanner object to take input from the user
Scanner myObj = new Scanner(System.in);
String userName;
// Display a message
System.out.println("Enter Your Name:");
// Read the user's input
userName = myObj.nextLine();
// Print and display the entered name
System.out.println("Your Name is: " + userName);
}
}
Input
Enter Your Name:
abhaymishra
Output
Your Name is : abhaymishra
"In the program above, the 'java.util' package is imported and used to run a basic program. These packages are commonly referred to as built-in or standard packages."
To establish a package in Java, adhere to the specified steps outlined below:
The first step in creating a Java package involves choosing a name for the package and placing this name at the beginning of your Java program's source code.
You can also include various components like classes, interfaces, annotation types, and more within this package. As an example, the following single statement is used to define a package named "FirstPackage."
Syntax: This statement is used to specify the name of the package you intend to create. The package statement serves the purpose of indicating the package to which the defined classes belong.
package FirstPackage ;
Choose a Name for the Package:
First, decide on a name for the package you want to create. This name should be a valid Java identifier, typically in lowercase, and follow the Java naming conventions, such as using reverse domain names for packages to ensure uniqueness.
Include the Package Statement:
In your Java source code file, add the package statement at the beginning of the file. The package statement declares the package name for the current Java file.
For example:
package com.example.mypackage;
Implementation: Creating a Class Within a Package
First declare the package name as the first statement of our program.
Following that, it is possible to include a class as a component of the package.
// Name of package to be created
package FirstPackage;
// Class in which the above created package belongs to
public class Welcome {
// Main driver method
public static void main(String[] args) {
// Print statement for the successful
// compilation and execution of the program
System.out.println("This Is The First Program Geeks For Geeks..");
}
}
Here, com.example.mypackage is the name of the package.
Create Classes and Other Elements:
Within your Java package, you can include classes, interfaces, annotation types, etc., as needed for your application. These classes should also specify the same package name in their respective package statements.
For example:
package com.example.mypackage;
public class MyClass {
// Class members and methods
}
Save the File:
Save the Java source file with the appropriate name, ensuring that the file is stored in the directory structure that matches the package name. In this example, the file should be stored in a directory structure like this: com/example/mypackage/MyClass.java.
Compile and Use:
Compile your Java files using a Java compiler (e.g., javac). Ensure that the directory structure matches the package hierarchy. After compiling successfully, you can use the classes and other elements from your package in other Java files.