Javatpoint Logo
Javatpoint Logo

How to Create Package in Java

In Java, a package is a group of classes, interfaces, enumeration, and annotations. Java contains many pre-defined packages such as java.lang, java.io, java.net, etc. When we create any Java program the java.lang package is imported by default. We need not to write the package name at the top of the program. We can also create our own package by providing the name that we want. In this section, we will learn how to create a package in Java.

We use package for the following reasons:

  • The package makes the search easier for the classes and interfaces.
  • It provides a fully qualified name that avoids naming conflicts.
  • It also controls access.
  • It organizes classes in a folder structure.
  • It improves code reusability.
  • Programmer can group classes and interfaces into a related package.

Creating a Package

To create a package, follow the steps given below:

  • Choose a package name according to the naming convention.
  • Write the package name at the top of every source file (classes, interface, enumeration, and annotations).
  • Remember that there must be only one package statement in each source file.

Package Naming Convention

We follow the naming convention rules to name a package. Java has some predefined packages and also allows us to create our own package. So, it is possible that a programmer can create a class with the same name as a package that already contains that type in a predefined package.

Let's take an example of the Rectangle class.

Suppose, a programmer creates a class with the name Rectangle in the package shape. The class with the same name is already present in java.awt package. The compiler allows both classes if they belong to the different packages. The fully qualified name of each class contains the package name that differentiate both Rectangle classes. Therefore, the package name of the user-defined class will be shape.Rectangle and the package name of the predefined class will be java.awt.Rectangle.

  • Package name must be in lower case that avoids conflict with the name of classes and interfaces.
  • Organizations used their internet domain name to define their package names. For example, com.javatpoint.mypackage. Sometimes, the organization also uses the region after the company name to name the package. For example, com.javatpoint.region.mypackage.
  • We use underscore in the package name if the domain name contains hyphen or other special characters or package names begin with a digit or reserved keyword.
Domain Name Package Name Prefix
Hyphenated-name.example.org org.example.hyphenated_name
Example.int int_.example
123name.example.com com.example._123name

Importing a Package

If we want to use a package in Java program it is necessary to import that package at the top of the program by using the import keyword before the package name.

Syntax:

Let's create a calculator program in Java using the package.

Add.java

Sub.java

Mult.java

Div.java

Now, we are going to create the main class named Calculator. In this class, we have imported all the packages that we have created above. It includes all the classes in the Calculator class.

Calculator.java

When we compile the above program, it creates corresponding .class files in packages named p1, p2, p3, p4, and p5, respectively.

How to Create Package in Java

The .class files are generated. Now, we can run the above program.

Output:

Enter your choice: 3
Enter the first number: 2
Enter the second number: 23	
Product=46





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA