How to create a package in java

Before moving further, you should have knowledge about the package in java.  In this article, we will explain how to create a package in java and covering some rules in java package creation.

Here is the table content of the article will we will cover this topic.
1. How to create a package in java?
2. Creating package in java (Without IDE)
3. Creating package with IDE(Eclipse)
4. java package naming convention
5. Adding a Class in the existing package
6. Adding a class in the package while importing another package

How to create a package in java?

The package in java which is created by the user is known as a User-defined package. It contains user-defined classes, enums, and interfaces.
There are two ways to create a package. If you are working with IDE like eclipse, then you don’t need to worry about the syntax of the package. But if you are not using any IDE then you have to follow some syntax. Here we will discuss them one by one.

Creating package in java (Without IDE)

In java, the Creation of the package is very simple. To create a package in java we need to use the package keyword and choose the name for the package. The declaration of the package should be in the first line of the program. After the declaration of the package, you can create class, enum, or interface.

To create a package in java we must follow two steps. In the first step, The declaration of the package and in the second step, the creation of the package by use of the command.

Step1: Syntax to declare a package in java.
package packageName;
Here package is a keyword and packageName is the name of the package.
package goal;

We can declare a package inside another package. Because of java support subdirectories.
package java.goal
Let’s take an example and see how we can use the package keyword in java. Here we will use the package keyword with the name of the package at the top of the Java source file.

package data;

public class Calculator
{
	public void calculate(int a, int b, char operation) 
	{
		switch(operation)
		{
			case '+'  :
				System.out.println("Addition of number : "+ (a+b));
                                break;
			case '*':
				System.out.println("Multiplication of number : "+ (a*b));
                                break;
			case '-':
				System.out.println("Subtraction of number : "+ (a-b));
                                break;
			case '/':
				System.out.println("Division of number : "+ (a/b));
                                break;
		}
	}
	public static void main(String arg[])
	{
		Calculator calculator = new Calculator();
		calculator.calculate(1, 2, '+');
		calculator.calculate(4, 2, '*');
		calculator.calculate(10, 5, '-');
		calculator.calculate(4, 2, '/');
	}
}

Output:Addition of number : 3
Multiplication of number : 8
Subtraction of number : 5
Division of number : 2

Here I have created a class Calculator that can perform the calculation for my application. So, I have created it inside a package name data. As we already discuss, to create a package with java source file, we need to use package keyword with the package name. Always remember the declaration should be the first statement in the source program.

Step2: Now we have declared the package in java source file.  Now we need to compile it. 

i) Save the file with java extension. Like Calculator.java
ii) Now compile the file with command javac Calculator.java After compilation of java file a class file Calculator.class is created. But there is no package is created yet. To create a package, we need to see next step.
iii) Now we must create a package, use the command
javac -d . Calculator.java

This command created a folder in a directory and place the class file in that folder. The folder name must be the same as the package name.

Creating package with IDE(Eclipse)

If you are using any IDE like eclipse then you don’t need to worry about the command or something else.  You just need to follow some steps in eclipse.

Right-click on your project and go to New > package. Now put the name of the package and click on the finish button.

how to create package in java

OR

Right-click on your project and go to New > class. A dialog will appear that asks for class and package name also.

how to create package in java

java package naming convention

1.  Always define the package name in lower case. All letter should be in lower case.

2. The name of package should be realistic. The name can determine by the company or organization that creates it.  We can determine the package name by reversing the company domain name.

3. If a package contains multiple name then it should be separated by dots(.)

4. The package names must be group of characters. Java does not recommend a package name with only single character.

5. Package names and class names should reflect business concepts. 

6. Package name must be unique (as a domain name). The  convention to create a package as a domain name, but in reverse order. For example, com.goal.java

Adding a Class in the existing package

We can include any class in existing package. To include a class inside an existing package, you should mention the package name with package keyword as the first statement in program. We can include a class in only one package at one time.

Let’s take an example from existing package. Here we will add a new class Utility.

package data;

public class Utility 
{
	public static void main(String arg[])
	{
		System.out.println("It is a utility class");
		System.out.println("We are adding this class in existing package");
		
	}
}

Output: It is a utility class
We are adding this class in existing package

As you can see, I have created a Utility class in existing a package named data. This package already contains a Calculator class. When we compile the Utility class, then Utility.class will be created in data folder.

Adding a class in package while importing another package

You must have seen in the above example we are adding a class in the existing package. But sometimes we need to import a class from other packages and also need to create a new package. In java, we can use the import statement to import the package and at the same time we can declare a new package also. But the declaration statement of the package should be in the first line.

package data;

import java.util.Date;

public class DateProvider 
{
	public void dateProvider()
	{
		System.out.println(new Date(2, 2, 2020));
	}
	
	public static void main(String arg[])
	{
		DateProvider provider = new DateProvider();
		provider.dateProvider();
	}
}

Output: Tue Sep 10 00:00:00 IST 1907

I have first declared the package data, then imported the class Date from the package java.util. We should follow the order when we are adding a class in the package and also importing another package. Package declaration should be the first statement then the import statement would be used.

Leave a Comment