In this article package in java we give the information about Package is a mechanism in which similar types are kept in a group. In Java, packages are used to organize classes and interfaces.
Package in Java:
In Java, a package is a group that contains classes, interfaces and sub-packages of similar type.
Define package in java:
“Package is a mechanism in which similar types are kept in a group.” In Java, packages are used to organize classes and interfaces.
There are two types of packages in Java. First built-in package and second user-defined (these are created by the user himself.)
Advantage of Package
- It is used to prevent naming conflicts. We can define two classes of the same name in different packages. For example, there can be two classes named Employee in two packages.
- By using Package we can reuse the code. We can import a class from one package and use it in another program.
- It provides access protection. Such as – protected class, default class and private class.
- It provides encapsulation i.e. through this we can hide the class.
- By using packages we can easily maintain our project and keep similar classes in one group. Later whenever we need, we can easily find any class.
Types of Package in Java:
In Java, there are two types of packages.
- Built-in package
- User-defined package
Built-in packages
These packages are already defined. Therefore these are also called predefined packages. There are many classes in it which are part of the java API. Below you are given the most commonly used built-in packages.
- java.lang – It contains language supported classes. This package is automatically imported.
- java.io – It contains classes that support input/output classes.
- java.util – It contains utility classes which implement the data structure.
- java.applet – It contains classes by which applets are created.
Import Package
Import keyword is used to import any package in Java. If you want, you can import all the classes present in the package together or you can also import any particular class. As-
import packageName.*; // import whole package.
import packageName.className; // import a single class from package.
Java Built-In Package Example
//For example we will try using java.util package –
// File Name: MyName.java
import java.util.Scanner; // import the Scanner class from java.util package.
class MyName
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
String name;
System.out.print(“Enter your name: “);
// capture entered name (string) from command line.
name = obj.nextLine();
System.out.println(“Your Name : ” + name);
}
}
Output:
Compile: javac MyName.java
Run: java MyName
Enter your name: Rajveer
Your Name: Rajveer
In the example, a program has been written to capture user input, in which java.util is a built-in package and Scanner is a class. In which nextLine() method has been used to read the complete line entered in the command line.
User-defined packages –
Those packages which are defined by the user are called user-defined packages.
Creating Package:
It is very easy to create a package in Java. For this, the package keyword is used and after that the name of the package is written.
Syntax:
package myPackage;
Example:- A simple example is given below.
//save as Stud.java
package Stud_info;
public class Stud
{
public static void main(String args[])
{
System.out.println(“This is my package”);
}
}
Output:-
Compile: javac Stud.java
Compile: javac -d . Stud.java
Run: java Stud_info.stud
This is my package
Importing packages in java:
There are three ways to import a package. Which are given below:-
- Importing all the classes of the package
- Importing a particular class of the package
- Using fully qualified name
1) Importing all the classes of the package
If we use import packagename.* then we can access all the classes and interfaces of this package. But we cannot use the classes and interfaces of subpackage.
Example:-
In the example below, we have created a class named First in the mypack package. And this class is accessed using import keyword in another class named Second.
// save by First.java
package mypack;
public class First
{
public void show()
{
System.out.println(“This is class First”);
}
}
//save by Second.java
package pack;
import mypack.*;
class Second
{
public static void main(String args[])
{
First obj = new First();
obj.show();
}
}
Output:-
First Compile: javac First.java
Compile: javac -d . First.java
2nd Compile: javac Second.java
Compile: javac -d . Second.java
Run: java pack.Second
This is class First.
2) Importing a particular class of the package
A package may have many classes and sometimes we want to access only one class, in this case we can do so by using import package.classname.
Example:-
In the example below, we have created a class named ClassOne in pack package. And we have accessed it in another class named ClassSecond.
//save by ClassOne.java
package pack1;
public class ClassOne
{
public void display()
{
System.out.println(“Have a nice Day!”);
}
}
//save by ClassSecond.java
package mypack2;
import pack1.ClassOne;
class ClassSecond
{
public static void main(String args[])
{
ClassOne obj = new ClassOne();
obj.display();
}
}
Output:-
First Compile: javac ClassOne.java
Compile: javac -d . ClassOne.java
2nd Compile: javac ClassSecond.java
Compile: javac -d . ClassSecond.java
Run: java mypack2.Second
Have a nice Day!
3) By using fully qualified name
When we use fully qualified name in our program, then only that particular class of the package can be accessed in the program.
Import keyword is not used in this. Generally, it is used when two packages have classes with the same name.
//save by A.java
package pack3;
public class A
{
public void msg()
{
System.out.println(“Hello world!”);
}
}
//save by B.java
package mypack5;
class B
{
public static void main(String args[])
{
pack3.A obj = new pack3.A(); //using fully qualified name
obj.msg();
}
}
Output:-
Hello world!
Some important points of Packages
- The package statement should be the first statement in the program.
- A package always has to be defined in a separate folder. And its name should be same as package name.
- All classes are stored in the package folder.
- All the classes of the package which we want to access outside the package should always be declared public.
- All classes of the package must be compiled before using.
- Sometimes two packages have a class with the same name. Due to which naming conflicts arise. To avoid this, we should use fully qualified name.
- Every class is part of some package. If we do not specify the package name then the class is placed in the default package.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining