Home » Uncategorized

Static Imports

3 February 2004 351 Views No Comment

I just read a summary of some of the features that will probably be available in Java 1.5.

On of the things discussed was static imports. In this example, an import statement is defined as static. Thereafter, all references to static fields in the class can be made without referencing the class from which they came from.



import static java.awt.Color.*;

public class ImportTest {

public static void main(String args[]) {

System.out.println(RED);

}

}

My initial thought on this is that it would make your code less readable. How can you tell easily (especially when the import statement has scrolled off the page) where the constant RED comes from? What if you have two static imports. How can you easily tell which class the reference comes from. It forces you to have to do some thinking and some digging in order to find out. In my mind, saying Colors.RED is not difficult and is amazingly more clear. The name of the class actually helps disambiguate the meaning of the constant. It provides it with context.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.