Java - Single-Type Import Declaration

What is Single-Type Import Declaration?

A single-type import declaration imports a single type from a package.

Syntax

It has the following form

import <fully qualified name of a type>;

Example

The following import declaration imports the Employee class from the com.book2s.demo package:

import com.book2s.demo.Employee;

A single-type import declaration imports only one type from a package.

The following import declarations import Class11 from pkg1 package, Class21 and Class22 from pkg2 package, and Class33 from pkg3 package:

import pkg1.Class11;
import pkg2.Class21;
import pkg2.Class22;
import pkg3.Class33;

The following code shows how to import Date class from java.util package.

Demo

import java.util.Date;
 class Main {/* w  ww  .  j a va 2 s. c  o  m*/
  public static void main(String[] args) {
    System.out.println(new Date());
  }
}

Result

You can reference the class by using its full qualified name.

public class Main {
  public static void main(String[] args) {
    System.out.println(new java.util.Date());
  }
}

Related Topic