Java Glossary : import

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : I words : import.

import
Because of the rigid source code naming convention, the Java compiler can easily find the corresponding source or class files just from the fully qualified name of a package and class. By fully qualified name I mean specifying the full package and class e.g.

java.util.ArrayList x = new java.util.ArrayList ( 149 );

The alternative to this long-winded style of coding, is to use import statements. A typical set of import statements might look like this:

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

They must come right after the package statement, before the class statement. They are traditionally kept sorted in alphabetical order. Then you can code in an abbreviated style:

ArrayList x = new java.util.ArrayList( 149 );

Unlike C or C++ we do not need to include headers to help the compiler determine what sorts of parameters other routines want; it can go look for itself in the source or class files. The import statement is not like the C++ include. So long as you fully qualify your reference in the code to class names with com.mindprod.mypackage.myClass there is no need for imports. They just allow shorthand. Even when you do have an import, you can still fully qualify your references to classes.

Let us say your package is called com.mindprod.mypackage and your class is called MyClass. There are two forms of the import statement:

  1. import com.mindprod.mypackage.MyClass;
  2. import com.mindprod.mypackage.*;
Then you can refer to the class as plain MyClass, static methods as MyClass.myStaticMethod() and static variables and constants as MyClass.myStaticValue, without the com.mindprod.mypackage qualification. There is no form of the import that lets you get away with coding your references without MyClass. e.g. just myStaticMethod() or myStaticValue. The most common problems with import are: For a discussion of the philosophy when to use imports and when to use qualification see the import tidier student project.

It is a great help to understanding someone else's code, (or even your own), if you refrain from using the .* style of import with the imports giving you an explicit list of the classes you use in that class. The list of explicit imports gives you a rough idea of what the class could do and what it is likely up to. Don't leave unused imports lying around. Use an import tidier to get rid of them, and sort the valid ones into alphabetical order. During development, it is fine to use the lazy .* form, (doing so will save you a ton of compiler errors), but once your work gels, convert over. Bon't camouflage your use of classes by fully qualifying them and not reporting them in the import. The only time you need qualification is when there is a name clash, e.g. java.awt.List and java.util.List.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 5297.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/import.html J:\mindprod\jgloss\import.html