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 that the import statement should be the second noncomment statement in your class file, following a package declaration.

You can import the static methods of a class using the new static import feature of Java 5.0:

import static java.lang.Math.*;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.63.87