Java 1.5 Tiger A Developers Notebook [Electronic resources]

David Flanagan, Brett McLaughlin

نسخه متنی -صفحه : 131/ 83
نمايش فراداده

8.1 Importing Static Members

If you've ever done any output in Java, you've typed System.out at least a few times. While there are lots of better ways to handle output, there is perhaps none as simple, direct, and to-the-point as good old System.out.println( ) and System.err.println( ). However, it's annoying to type System.out and System.err time and time again (at least, it is for me). In Tiger, this annoyance is gone, via static imports.

NOTE

If you try and import something non-static with this syntax, all you get is a "cannot find symbol" compiler errorit's not very descriptive.

8.1.1 How do I do that?

Anytime you have a static member, like out and err in the java.lang. System class, you can import those static members into your code:

import static java.lang.System.err;
import static java.lang.System.out;

You just use import static instead of import. Magically, your code can now use the methods of these static members without prefacing those methods with the static member's package name, as seen in Example 8-1.

Example 8-1. Using statically imported member variables
package com.oreilly.tiger.ch08;
import static java.lang.System.err;

import static java.lang.System.out; public class StaticImporter { public static void main(String[] args) { if (args.length < 2) { err.println( "Incorrect usage: java com.oreilly.tiger.ch08 [arg1] [arg2]"); return; } out.println("Good morning, " + args[0]); out.println("Have a " + args[1] + " day!"); } }

As said before, this isn't anything particularly revolutionary, but it's certainly a nice bonus to the language.

As an added bonus, this same thing applies to static methods, in addition to static variables. For example, consider the java.util.Arrays class, which has several static methods, such as binarySearch( ), sort( ), and equals( ). These methods are just static members of the class, and can be imported, just like the err and out members in Example 8-1:

NOTE

Note that Arrays.sort( ) is a heavily overloaded method. This import directive imports the method by its name, and not any particular overloading of it.

     import static java.util.Arrays.sort;

You can now just code as shown here:

     sort(myObjectArray); // no need to use Arrays.sort( )

This is a particularly nice improvement to the language, I think.

8.1.2 What about...

...static member types? A member type is a type defined within another class. For example, the java.lang.Character class defines a class within its body, called Subset. The formal name of this class, because it does not stand on its own, is Character.Subset (and it appears that way within the JavaDocs). You can import a member type, in Tiger and in previous versions, with a normal import statement:

     import java.lang.Character.Subset;

This is a little-known, little-used feature of Java that has nothing to do with Tiger. However, where Tiger does come in is adding the ability to do static imports. In the case of Character.Subset, not only is Subset a member type, it's a static member typemeaning that it could also be imported as seen here:

NOTE

Anonymous inner classes defined within a method are not members, and cannot be imported.

     import static java.lang.Character.Subset;

So, which is correct? Actually, both. However, you shouldn't have both in the same codejust one or the other. Otherwise, compilers will gripe about a name conflict (two identically named types). Personally, I like the clarity of import static for this sort of thing, as it reminds me that Character.Subset is staticbut that's style more than any real best practice.