How to Create a Package in Java? - GeeksforGeeks

01 Nov, 2021
Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. All we need to do is put related classes into packages. After that, we can simply write an import class from existing packages and use it in our program. A package is a container of a group of related classes where some classes are accessible are exposed and others are kept for internal purposes. We can reuse existing classes from the packages as many times as we need them in our program. Package names and directory structure are closely related
Ways: There are two types of packages in java:
User-defined Package (Create Your Own Package’s)Built-in packages are packages from the java application programming interface that are the packages from Java API for example such as swing, util, net, io, AWT, lang, javax, etc.
User-defined Package (Create Your Own Package’s)
Built-in packages are packages from the java application programming interface that are the packages from Java API for example such as swing, util, net, io, AWT, lang, javax, etc.
In this article, we will see How To Create A Package In Java?. A package is a group of similar types of Classes, Interfaces, and sub-packages. We use Packages in order to avoid name conflicts. 
Syntax: To import a package
import package.name.*;
Example: To import a package
Java
// Java Program to Import a package // Importing java utility packageimport java.util.*; // Main Classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Scanner to take input from the user object        Scanner myObj = new Scanner(System.in);        String userName;         // Display message        // Enter Your Name And Press Enter        System.out.println("Enter You Name");         // Reading the integer age entered using        // nextInt() method        userName = myObj.nextLine();         // Print and display        System.out.println("Your Name IS : " + userName);    }}
Enter You Name
Your Name IS : 0
Here In The Above Program, ‘java.util’ package is imported and run for a simple program. These are called as Inbuilt Packages. 
Now in order to create a package in java follow the certain steps as described below: 
First We Should Choose A Name For The Package We Are Going To Create And Include. The package command In The first line in the java program source code.Further inclusion of classes, interfaces, annotation types, etc that is required in the package can be made in the package. For example, the below single statement creates a package name called “FirstPackage”.
First We Should Choose A Name For The Package We Are Going To Create And Include. The package command In The first line in the java program source code.
Further inclusion of classes, interfaces, annotation types, etc that is required in the package can be made in the package. For example, the below single statement creates a package name called “FirstPackage”.
Syntax: To declare the name of the package to be created. The package statement simply defines in which package the classes defined belong.
package FirstPackage ;
Implementation: To Create a Class Inside A Package 
First Declare The Package Name As The First Statement Of Our Program.Then We Can Include A Class As A Part Of The Package.
First Declare The Package Name As The First Statement Of Our Program.
Then We Can Include A Class As A Part Of The Package.
Example 1: 
Java
// Name of package to be createdpackage FirstPackage; // Class in which the above created package belong toclass 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..");    }}
So Inorder to generate the above-desired output first do use the commands as specified use the following specified commands
Procedure: 
1. To generate the output from the above program 
Command: javac Welcome.java 
2. The Above Command Will Give Us Welcome.class File. 
Command: javac -d . Welcome.java 
3. So This Command Will Create a New Folder Called  FirstPackage. 
Command: java FirstPackage.Welcome 
Output: The Above Will Give The Final Output Of The Example Program
This Is The Output Of The Above Program
Example 2: 
Java
// Name of package to be createdpackage data; // Class to which the above package belongspublic class Demo {     // Member functions of the class- 'Demo'    // Method 1 - To show()    public void show()    {         // Print message        System.out.println("Hi Everyone");    }     // Method 2 - To show()    public void view()    {        // Print message        System.out.println("Hello");    }}
Again, in order to generate the above-desired output first do use the commands as specified use the following specified commands
Procedure: 
1. To generate the output from the above program 
Command: javac Demo.java 
2. This Command Will Give Us a Class File  
Command: javac -d . Demo.java 
3. So This Command Will Create a New Folder Called data. 
Note: In data Demo.java & Demo.class File should be present 
Example 3: Data will be tried to be accessed now from another program 
Java
// Name of the packageimport data.*; // Class to which the package belongsclass ncj {     // main driver method    public static void main(String arg[])    {         // Creating an object of Demo class        Demo d = new Demo();         // Calling the functions show() and view()        // using the object of Demo class        d.show();        d.view();    }}
Again the following commands will be used in order to generate the output as first a file ill be created ‘ncj.java’ outside the data directory.
Command: javac Demo.java 
The Above  Command Will Give us a class file that is non-runnable so we do need a command further to make it an executable run file.  
Command: java ncj 
// To Run This File 
Output: Generated on the terminal after the above command Is executed 
Hi Everyone
Hello 
arorakashish0911
addyjeridiq
Java-Packages
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character