How to Import Packages in Java
To know about the importing the packages of Java, we need to understand about how to packages work.
Packages
The package in Java is a collection of Classes and Interfaces. The packages are used to avoid naming conflicts and to reduce the complexity of writing the difficult code. The packages give the access control.
The packages in Java are classified into two types
- Built-in Packages / Predefined Packages
- User-defined packages
Built-in / Predefined packages:
The Built-in/Predefined packages are the packages which are already written by the developers of Java. These are given with the JDK. There are various predefined/ Built-in packages, some are mentioned below:
- java.lang – It contains classes and interfaces that are given fundamental with java
- java.util – It contains the Collection Frameworks like ArrayList, LinkedList , Calendar, Date etc.
- java.io – It contains the input and output operations.
- java.awt – It contains Graphical User Interfaces used for painting,graphics and Images.
- java.net- It used for the networking applications.
User-defined Packages
The User defined packages are the packages that are created by the developers.
In the creation of user-defined packages, the “Package” keyword is used.
Syntax:
pack package_name;
Example:
//Example program for User-defined packages
package kotte;
public class Demo
{
public static void main(String args[])
{
System.out.println(“Demo Package”);
}
}
Compile Java Package:
To compile the Java package we use commands like “javac -d . package_name.java”. The “-d” used to specify the path to her class file stored.
Import a Package in Java.
To import the package in Java, the “import” keyword is used.
Example:
import java.lang.*;
Access packages:
There are 3 ways of accessing packages
- import package_name.*;
- import package_name.classname;
- fully qualified name.
Using “import package_name.*;”
The “import package_name.*;” is used to access to all the classes and interface that are included within that package.
Example:
package kotte;
public class Demo
{
public void show()
{
System.out.println(“Welcome”);
}
}
// class with demo
import kotte.*;
class Pdemo
{
public static void main(String args[])
{
PDemo d = new PDemo();
d.show();
}
}
Output:
Welcome
In the above program we created a package “kotte” . by using th import keyword we are importing the “kotte” package in Xdemo class.
Using “import package_name.classname;”
The “import package_name.classname;” is used to access to the specific classes and interface that are included within that package.
Example:
package kotte;
public class Demo
{
public void show()
{
System.out.println(“Welcome”);
}
public void display()
{
System.out.println(“Bye”);
}
}
public class ExDemo
{
public void hi()
{
System.out.println(“Greetings”);
}
}
// class with demo
import kotte.Exdemo;
class Xdemo
{
public static void main(String args[])
{
ExDemo d = new ExDemo();
d.hi();
}
}
Output:
Greetings
In the above program we created a package “kotte” . In that package there are two different classes . By using the import keyword we are importing the “kotte.Exdemo” we are specifying the class that are present in that package.
Using Fully Qualified Name:
With the help of using the fully qualified name we do not need the import keyword. In this type accessing the package is “package_name.Classname” format.
Example:
package kotte;
public class KxDemo
{
public void show()
{
System.out.println(“Welcome”);
}
}
// class with demo
class Xdemo
{
public static void main(String args[])
{
kotte.Demo d = new kotte.Demo();
d.show();
}
}
Output:
Welcome
In the above program we didn’t use any import keyword, but we have to specify the package name before the creating an object of a class like (package_name.classname).