Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHow to Create a Package in Java?

How to Create a Package in Java?

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:

  1. User-defined Package (Create Your Own Package’s)
  2. 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 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 Your Name:");
 
        // Reading the integer age entered using
        // nextInt() method
        userName = myObj.nextLine();
 
        // Print and display
        System.out.println("Your Name is : " + userName);
    }
}


Input

Enter Your Name:
neveropen

Output

Your Name is : neveropen

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: 

  1. 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.
  2. 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 

  1. First Declare The Package Name As The First Statement Of Our Program.
  2. Then We Can Include A Class As A Part Of The Package.

Example 1: 

Java




// 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 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 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.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 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 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 

RELATED ARTICLES

Most Popular

Recent Comments