The import Statement
Home ] Up ] Qualified Names ] [ The import Statement ] Automatic Import ] The package Statement ] Compilation Units ] Host Support for Packages ] Type Definitions ] Access Control ]

 

Static Imports

 

It's a royal pain to have to fully qualify every class name in your program.

So the Java folks added the import statement as a convenience:

import java.util.Hashtable; // import just HashTable

or:

import java.util.*; // import all classes in java.util

which allows the Java compiler to make a name accessible through an abbreviated name -- the class name itself (in this case Hashtable).

Note: I try to discourage the use of the '*' method of importing, because I consider it bad -- often sloppy/lazy -- software engineering practice.  Please don't use it, at least in my course!

If you wish, you can use a mix of import statements and fully qualified references.

If two packages imported in this way contain classes with the same name, you cannot use either of those classes without using the fully qualified name for each.

Note:  Remember:
  • There may be any number of import statements in a Java program.
  • They must follow any package statement in the source
  • They must precede the first class definition (or interface definition -- more later) in the file.
 

This page was last modified on 02 October, 2007