I am rewriting a Java book for beginners, and it seems to make so much sense to use
import static java.lang.System.out; public class Greeting { public static void main(String[] args) { out.println("Hello, World!"); } }
I would no longer have to
dissect the awful
System.out.println("Hello, World!")
expression.
(Ok, boys and girls. System
is a class. System.out
is
a static field of type PrintStream
. That's another class. What is
static, you ask? It's what a TV set makes when it doesn't get a signal. No,
it's what you don't want in your dryer. Ok, whatever it is, you don't ever want
to do it in your code, unless it is also final
. That means, you
can't change it. Oh, you spotted System.setOut
.
Next question?)
Ditto with mathematical expressions. Doesn't
sin(angle * PI / 180)
look so much nicer than
Math.sin(angle * Math.PI / 180)
Ok, at least to that tiny fraction of the population who knows trigonometry...
The fact is, I don't recall ever seeing anyone using static import outside
JUnit 4, where it is justifiably popular to import
org.junit.Assert.*
.
I made a Google search, only to find a sensible blog whose comments were filled with rants that static imports are the work of the antichrist, something on static imports in C#, a recommendation to use it rarely, and the usual certification garbage (here and here).
I am trying to get the reaction of the average Java coder here. Have you
switched from System.out
to out
with a static import?
Would you think it weird to look at other people's code that did that? Or would
you welcome it?