Import Declaration

The import command allows you to reference a class in the imported package only by its class name instead of having to type the fully qualified name every time you reference it in your class. For instance, java.lang.String is the fully qualified name for the String class. The String class is contained in the java.lang package. Because java.lang is always imported automatically, we are free to refer to a String without prefixing it with its package name.

To import all of the classes in a package, use a wildcard.

import java.sql.*;
// now you can refer to any class in the
// java.sql package by class name only in your code:

class myClass {
try {
    //look, ma! no package!
    Statement stmt = con.createStatement();
    //....
}...

Note ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.