How to Create a Package in Java?
Last Updated :
18 Apr, 2023
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 or 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.
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
String userName;
System.out.println( "Enter Your Name:" );
userName = myObj.nextLine();
System.out.println( "Your Name is : " + userName);
}
}
|
Input
Enter Your Name:
geeksforgeeks
Output
Your Name is : geeksforgeeks
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”.
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.
Example 1:
Java
package FirstPackage;
class Welcome {
public static void main(String[] args)
{
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
package data;
public class Demo {
public void show()
{
System.out.println( "Hi Everyone" );
}
public void view()
{
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
import data.*;
class ncj {
public static void main(String arg[])
{
Demo d = new Demo();
d.show();
d.view();
}
}
|
Again the following commands will be used in order to generate the output first a file will 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
Share your thoughts in the comments
Please Login to comment...