×

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide a very efficient mechanism for alignment of various files. The files which are related to each other can be stored in the same package, and whenever there is a need to access the related files, we just have to go through one package. A unique namespace is provided by the package. A package can contain the following types of file in java:
  • Class
  • Interface
  • Enumerated types
  • Annotations

Advantages of packages

  • Two classes in two different packages can have the same name so, name collision issue is not a problem.
  • Classes related to each other are stored in the same package so, accessing them is easy.
  • Access protection is provided by packages so, no need to worry about the security concerns. The files that are crucial can be hidden successfully.
  • Programmer can make their own packages as per their convenience. It is programmer friendly.
  • Code reusability is provided.

Types of packages in Java

Built-in packages: They are the ones which are predefined by the java compiler. They can be imported to use by the user. User-defined packages: They are the ones which are made by the programmer to group related classes, enumerations, annotations and interfaces as required by him.

Built-in packages in java.

There are some built-in packages in java that can be used as it is by the programmer. They are listed below. Let us take a look at each one of these packages:
  1. java.lang: This package contains language support classes. They are by default imported in the java programs.
  2. java.util: This package contains language utility classes such as vectors, lists, hash tables, etc.
  3. java.awt: This package contains a set of classes for implementing the graphic user interface.
  4. java.applet: This package contains a set of classes for using applets in java program.
  5. java.net: This package contains a set of network classes.
  6. java.io: This package contains classes for input and output support.

User-defined packages

Having seen the predefined packages, now it's time to see how the packages can be created, imported and used by the users. Creating Packages
  • Select a suitable name for the package.
  • Declare the name of the package with the 'package' keyword in the first line of a program. E.g. package firstpack.
  • Define a public class inside that package.
  • Create a subdirectory in the main directory to store the class. Save it as classname.java.
  • Compile the file.
E.g.
package packagename;
public class classname;
{
.................
}
Importing Packages After the creation of packages, it can be imported using the 'import' keyword. Packages can be imported in two ways.
  • By explicitly declaring the class name to be imported: E.g. import firstpackage.secondpackge.classname;
  • By importing all the classes in a package. E.g. import packagename.*;
The drawback of using the shortcut approach is that it is difficult to determine from which class a particular member came. While using the shortcut approach, all the classes get imported. If the programmer wishes to hide some of the class then it is advised to use access modifier other then public for that class. Using Packages After having seen how packages are created and imported, let us have a look at how they are used. In the program given below, we will see all the steps mentioned.
package pkg1; //creating package
public class Calculator
{
int x;
int y;
public int add(int a,int b)
{
x=a;
y=b;
return x+y;
}
public int sub(int c,int d)
{
x=c;
y=d;
return x-y;
}
public int mul(int e,int f)
{
x=e;
y=f;
return x*y;
}
public int div(int g,int h)
{
x=g;
y=h;
return x/y;
}
}
pack1 is the user-defined package, and 'Calculator' is the name of the class. The program is saved with the name Calculator.java. Once defined, let's see how the package is used.
import pkg1.Calculator; //importing package
class Operate
{
public static void main(String args[])
{
Calculator obj1 = new Calculator();
obj1.add(5,6);
Calculator obj2 = new Calculator();
obj2.sub(8,6);
Calculator obj3 = new Calculator();
obj3.mul(5,6);
}
}

Program to import two packages simultaneously.

package pkg1;
public class Calculator
{
int v=45;
public void show()
{
System.out.println("Hy!! value of v is"+v);
}
}
Now, we will make another class which will import both the packages.
import pkg1.Calculator;
import pkg2.ClassA;
class Test
{
public static void main(String args[])
{
Calculator obj1 = new Calculator();
obj1.add(5,6);
ClassB obj2 = new ClassB();
obj1.show();
}
}
This class uses both the packages simultaneously. We can import as many packages as we desire.

How to add a new class to an existing package?

There is an example given below to add a new class to an existing package.
package p1;
public class One
{
..............
}
This is the package in which we wish to add a new class, and it can be done as:
  • Define the class and use a public modifier.
  • Place the package statement at the top of the program.
  • Compile the class as done earlier.
  • The class is now added to the package.
package p1
public class Two
{
................
}

Related Topics

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.