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.*; 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net