0

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

  • 1
    Possible duplicate of [What does the "static" modifier after "import" mean?](https://stackoverflow.com/questions/162187/what-does-the-static-modifier-after-import-mean) – Logan Aug 09 '18 at 05:25
  • I guess it is being considered static since it is being accessed through a static member but println is not actually a static method in the PrintStream class. Is this the correct way to interpret the statement? –  Aug 09 '18 at 05:25
  • You are correct, `println` is definitely **not** a `static` method by anyone's definition and certainly not by the JLS. It is an instance method of a `static` member of the `System` class. Line 4, however, `toHexString` _is_ a `static` method. – Boris the Spider Aug 09 '18 at 05:29
  • `static` methods can only be referenced directly from other `static` methods. – dryleaf Aug 09 '18 at 05:31
  • @dryleaf what does that even mean? If course I can call `static` method directly from an instance method! Or from a ctor! – Boris the Spider Aug 09 '18 at 05:31
  • Means that, because your `main` method is `static`; If you want to call a method within main without its object, then that method should be static. That's why you can use method `out` (from `System`), in `main` method, because you are importing it as `static` – dryleaf Aug 09 '18 at 05:34
  • @dryleaf I believe your understanding of what `static` means is deeply flawed - you seem to have gotten your causation backwards. "_`static` methods can only be referenced directly from other `static` methods_" should be "`static` methods can only directly reference other `static` methods". As to your second comment, the `static` import really has absolutely zero to do with `main` being `static` - it just allows shortcutting the referencing the class of the imported `static`. – Boris the Spider Aug 09 '18 at 05:39

3 Answers3

3

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.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
1

'import static' can only refer static members of a Class.
So here it is refering 'out' Object from System class.
In System class 'out' is defined as

  public final static PrintStream out = null;

println() is non static method of java.io.PrintStream class.

So here 'import static' is nothing to do with println() it is only refering 'out' object.
And 'out' is further refering to println().

Same with Integer class. it is importing all static methods and variables of Integer class. So you can call it directly as

out.println(MAX_VALUE);  

instead of

out.println(Integer.MAX_VALUE);
Tarun
  • 986
  • 6
  • 19
-1

The method being referred to as static is toHexString, not println. What that line means is that they were able to import toHexString and MAX_VALUE with a single statement (import static java.lang.Integer.*;).

Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • "_The book says of line 3: "So in this line of code..._" - so you are agreeing that the book is wrong? I don't think this sentence can in any way be interpreted to refer to line 4 as well as line 3. – Boris the Spider Aug 09 '18 at 05:31
  • @BoristheSpider the "line 3" being referred to is the _import statement_, not the usage. You can import static methods and constants in Java. The book is saying you don't have to do both separately but can use the asterisk shortcut instead. – Leo Aso Aug 09 '18 at 05:35
  • That's not what the labelling says, or what counting says. – Boris the Spider Aug 09 '18 at 05:36
  • Well then maybe the OP didn't post the code exactly as in the book, or maybe they left out something, but for me it was clear they were referring to the import statement. – Leo Aso Aug 09 '18 at 05:40
  • This assumption requires clarification from the OP that they got the text incorrect. As it stands this answer is incorrect. – Boris the Spider Aug 09 '18 at 05:42
  • 1
    Quoted from the book: _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._ – Adrian Shum Aug 09 '18 at 05:51
  • I guess what OP criticized is actually valid – Adrian Shum Aug 09 '18 at 05:51
  • @AdrianShum good job on finding the actual quote. If you turn that into an answer, I'd vote for it. – Boris the Spider Aug 09 '18 at 05:53