Hacker News new | past | comments | ask | show | jobs | submit login

But note the warning from Sun: "So when should you use static import? Very sparingly!"

I'm so glad there is an authoritative answer on this subject. IMHO, I hate the use of static imports because it becomes difficult to know where a particular method comes from... where it's an instance method from the class itself or one that is brought in through a static import.

And then there's the possibility of clobbering the namespace by including a local variable name and a statically imported variable.




I don't like them in general either, but there's one place I use them extensively: JUnit tests (statically import Mockito, Hamcrest, Assert, etc).

It mostly has to do with how much the library tries to be a DSL, and how much readability will suffer if I don't use a static import. IMO, this

  assertThat(6, isGreaterThan(5));
reads a heckuva lot better than

  Assert.assertThat(6, Matchers.isGreaterThan(5));
Of course the combination of poor Java type inference and Generics-heavy libraries make it so that often I have to do the latter to make use of angle braces, but that's another story.


It really should be Assert.that(6, Is.greaterThan(7)), shouldn't it? Best of both worlds!


I don't understand this comment at all.

Do you not know that import statements are at the top of every class file, or did you mean that you hate wildcard imports? Do you also eschew local variables, because they might clobber fields and method parameters if you ignore the compiler warnings?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: