Skip to content
Related Articles

Related Articles

How to Create a Package in Java?
  • Last Updated : 19 Jan, 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

There are two types of packages in java:

  1. User-defined Package (Create Your Own Package’s)
  2. Built-in packages which are packages from 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
  
        // Readig the integer age entered using
        // nextInt() method
        userName = myObj.nextLine();
  
        // Print and display
        System.out.println("Your Name IS : " + userName);
    }
}

chevron_right


Output

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:

  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 name of package to be created. The package statement simply defines in which package the classes defined are belonged.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..");
    }
}

chevron_right


So Inorder to generate the above-desired output first do use the commands as specified use the following specified commands



Procedure: To generate the output from above program

Command: javac Welcome.java 

The Above Command Will Give Us Wellcome.class File.

Command: javac -d . Welcome.java 

 So This Command Will Create a New Folder Called  FirstPackage.

Command: java FisrtPackage .Welcome 

 Output: The Above Will Give The Final Output Of The Example Program

This Is The Output Of The Above Program

Example 2:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Name of package to be created
package data;
  
// Class to which the above package belongs to
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");
    }
}

chevron_right


Again, in order to generate the above-desired output first do use the commands as specified use the following specified commands

Procedure: To generate the output from above program

Command: javac Demo.java 

This Command Will Give Us a Class File 

Command: javac -d . Demo.java 

 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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 class File 

Command: java ncj //To Run This File 

Output: After The Above Command Is Executed

This Is The Output Of The Example Program:2


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :