How to import a package in Java?

To use a package, specifying the location of a package is a time taking job. Hence, import concept is used. A package is imported for making a class or a package visible to a program.

Syntax 1: Importing an explicit class name

import package1.package2.classname;

For Example:

import java.util.scanner

Syntax 2: Importing an entire package

import package1.package2.*;

For Example:

import java.awt.*;

where, package1 is a top package and package 2 is a subordinate package. You can use any hierarchical structure to get the desired package.

Leave a Reply