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:
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 Program to Import a package // Importing java utility package import java.util.*; // Main Class class 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:
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
Example 1:
// Name of package to be created package FirstPackage; // Class in which the above created package belong to 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 programming shark.." ); } } |
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.java2. The Above Command Will Give Us Welcome.class File.
Command: javac -d . Welcome.java3. So This Command Will Create a New Folder Called FirstPackage.
Command: java FirstPackage.WelcomeOutput: The Above Will Give The Final Output Of The Example Program
This Is The First Program programming shark..
Example 2:
- Java
// Name of package to be created
package
data;
// Class to which the above package belongs
public
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.java2. This Command Will Give Us a Class File
Command: javac -d . Demo.java3. 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 package
import
data.*;
// Class to which the package belongs
class
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 will be created ‘ncj.java’ outside the data directory.
Command: javac Demo.javaThe 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 FileOutput: Generated on the terminal after the above command Is executed
Hi Everyone Hello
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.