How to Create and use Packages in Java

In the previous tutorial, we learned about the Data manipulation through Wrapper Classes in Java with examples. In this tutorial, we will learn about How to Create and use Packages in Java.

317

In this article, we will discuss What is a Package in Java, How to create a package, and the use of packages in Java with examples.

What is a Package in Java?

Generally, while working in a system, we categorize different items, and the items which have similar functionality are kept with each other. For example, our computer has different songs, movies, files, etc. So, to make our work easier we make a folder of songs and keep all the songs in it. Similarly, for movies and other things.

The same concept is implemented in Java too. While working on a project, the programmers create packages that contain different files according to their functionality and usability.

Files in one directory (or package) would have different functionality from those that are present in another package.

For example: There is a predefined package named java.io which contains the files related to input/output.

One of the benefits of creating packages in Java is that it prevents the problem of name collision. It is possible that there are different classes in Java with the same name. So, this will lead to a name collision between those two. But, if we keep these two classes in different packages then there is no problem.

There are several benefits of packages which include ease of maintenance, organization, and increase collaboration among developers.

Creating and using packages in java

How to create a package?

While creating the file, we need to use the package keyword to make the file part of the package.

We will understand this better with the help of the code given below –

package mypackage;
public class Hello{
public static void main (String [] args) {
System.out.println(“Hello World”);}
} 

When we generally compile a program, we use the following syntax –

javac filename.java

This will compile our code. But, if we have used the package keyword, this will not create the folder containing our class that we desire to with the use of the package.

To do this, we have to use the following syntax –

javac -d . filename.java

And, while running the program, we have to use the following syntax –

java package_name.filename

How to use Packages in Java

To use a class from a different package, we use the import keyword.

In the below example, we will create a class in a different package and create its object in another package. So, let’s understand this with the help of the code given below –

First, we are creating a class Student in package 2 –

package pack2;
public class Student{
private int rollno;
private string name;
public void setRollno(int r)
{rollno = r;}
                                   public void setName(String n)

                                                    {name = n;}

                           public void getRollno(){return(rollno);}

                          public String getName(){return(name);}

}

Now, we will create another package in which we will be using the class of the above created package.

package pack1;
import pack2.Student;
public class Example{
public static void main(String[] args){
Student s1 = new Student();
s1.setRollno(25);
s1.setName(Harshit);
System.out.println(“Roll No:” + s1.getRollno());
System.out.println(“Name:” + s1.getName());
}
}

Compilation –  

For the above program to execute, during compilation we will use the following syntax –

javac -d . Student.java
javac -d. Example.java 
java pack1.Example

Output –

Roll No: 25
Name: Harshit

While compiling –

So, in this article, we have learned about the use of the ‘package’ and ‘import’ keywords in Java.

Related Quiz:

Previous articleData manipulation through Wrapper Classes in Java with examples
Next articleEnvironmental Engineering By Dr Swati Agrawal, Kalinga University
Harshit Brijwasi
I am Harshit Brijwasi. I am an engineer and a Technical Content writer. I am here to share my knowledge about technical topics and help researchers with them.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.