Certification Objective Using Static Imports (Exam Objective 7.1)


Certification Objective —Using Static Imports (Exam Objective 7.1)

7. 1 Given a code example and a scenario, write code that uses the appropriate access modifiers, package declarations, and import statements to interact with (through access or inheritance) the code in the example.

Note: In Chapter 1 we covered most of what's defined in this objective, but we saved static imports for this chapter.

Static Imports

We've been using import statements throughout the book. Ultimately, the only value import statements have is that they save typing and they can make your code easier to read. In Java 5, the import statement was enhanced to provide even greater keystroke-reduction capabilitiesalthough some would argue that this comes at the expense of readability. This new feature is known as static imports. Static imports can be used when you want to use a class's static members. (You can use this feature on classes in the API and on your own classes.) Here's a "before and after" example:

Before static imports:

 public class TestStatic {   public static void main(String[] args) {     System.out.println(Integer.MAX_VALUE);     System.out.printl.n(Integer.toHexString(42));   } } 

After static imports:

 import static java.lang.System.out;             // 1 import static java.lang.Integer.*;              // 2 public class TestStaticImport {   public static void main(String[] args)  {     out.println(MAX_VALUE);                     // 3     out.println(toHexString(42));               // 4   } } 

Both classes produce the same output:

 2147483647 2a 

Let's look at what's happening in the code that's using the static import feature:

  1. Even though the feature is commonly called "static import" the syntax MUST be import static followed by the fully qualified name of the static member you want to import, or a wildcard. In this case we're doing a static import on the System class's out object.

  2. In this case we might want to use several of the static members of the java.lang.integer class. This static import statement uses the wildcard to say, "I want to do static imports of ALL the static members in this class."

  3. Now we're finally seeing the benefit of the static import feature! We didn't have to type the System in System.out.println! Wow! Second, we didn't have to type the Integer in Integer.MAX_VALUE. So in this line of code we were able to use a shortcut for a static method AND a constant.

  4. Finally, we do one more shortcut, this time for a method in the Integer class.

We've been a little sarcastic about this feature, but we're not the only ones. We're not convinced that saving a few keystrokes is worth possibly making the code a little harder to read, but enough developers requested it that it was added to the language.

Here are a couple of rules for using static imports:

  • You must say import static; you can't say static import.

  • Watch out for ambiguously named static members. For instance, if you do a static import for both the Integer class and the Long class, referring to MAX_VALUE will cause a compiler error, since both Integer and Long have a MAX_VALUE constant, and Java won't know which MAX_VALUE you're referring to.

  • You can do a static import on static object references, constants (remember they're static and final), and static methods.




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net