Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, November 19, 2007

how to use java package qualifiers instead of imports

The import statement is not required. Class references can be made but explicit qualification with the "." operator. For example,

java.util.ArrayList studentNames; // fully qualified.  No need for import.

The fully qualified style is used in some textbooks, but is generally not used when programming, where the import style is almost universal.

However, there is one situation where qualification is necessary - when two classes have the same name, but are in different packages. For example, there is both java.util.Timer and java.swing.Timer. Because it's common to import all classes in both java.util and javax.swing, the name Timer is ambiguous and can't be used without qualification.

import java.util.*;    // The java.util package contains a Timer class.
import javax.swing.*; // The javax.swing package also contains a Timer class.
. . .
Timer t; // AMBIGUOUS - compilation error
java.util.Timer t; // OK, you've told the compiler which one you want.

No comments:

Post a Comment

Archives