How to Import a Package in Java
What is a Package
In java, we can group a number of classes into a single unit known as packages which also act as a container for a group of classes. Packages resolve names conflicts in the class names. Different packages can contain the classes with the same names. We can import a package using the import statement.
Example:
There are several inbuilt java packages.
java.lang.*;
java.io.*;
java.net.*;
java.util.*;
Define a Package
A programmer can create his own package to group related classes of the development effort.
Syntax :
package name;
A class that defines inside a package “com.testingdocs.tutorial” should be placed inside the directory i.e “com\testingdocs\tutorial”.
In this way, a class hierarchy can be built. A hierarchy of packages can be created to better organize the classes in the application development or automation framework development.
Creating a Package
Creating a package in Java is simply creating a package command followed by the name of the package as the first name in the Java source file.
Example
package com.testingdocs.tutorial;
Import a Package
Example:
package com.testingdocs.tutorial;
public class Test
{
String testName;
String testID;
}
In the above example using the statement will create a package with folder com.testingdocs.tutorial in the project directory. We can import this class in another class using the import statement.
For example: Importing a class in a package
import com.testingdocs.tutorial.Test;
Importing the whole package using the star notation:
import com.testingdocs.tutorial.*;