javastatic-membersstatic-import

Are methods of static members considered static?


In the following static import example from pg. 16 of the Oracle OCA/OCP Java SE 7 Programmer I and II Study Guide:

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
  }
}

The book says of the line marked 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.

Is it an error to refer to println as a static method here?

The program above is given as shown in the text.

For the line marked 4, the book says: "Finally, we do one more shortcut, this time for a method in the Integer class."


Solution

  • Quoted from the book:

    1. 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.

    Your criticism is valid. In that line of code, we are NOT using a shortcut for static method. It is just a shortcut to a static field instead.