10

What's the good point to use "import static"?

user496949
  • 83,087
  • 147
  • 309
  • 426

2 Answers2

10

In general you should use static imports very sparingly.

One of the few places that they make a lot of sense is in your unit tests:

import static junit.framework.Assert.*;

Static imports were added to Java to stop programmers from implementing the Constant Interface Antipattern.

dbyrne
  • 59,111
  • 13
  • 86
  • 103
  • 1
    Link also says: "Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names." – Will Mar 04 '11 at 07:10
  • Of course they were added because they are sometimes useful, but using them too little will cause much less damage than using them too much. – dbyrne Mar 04 '11 at 07:14
  • They can do damage? Sounds a little exaggerated. – user unknown Jun 02 '11 at 01:53
6

It allows you to remove the class name from function calls for static methods, as described with examples in the documentation here: http://download.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

Will
  • 73,905
  • 40
  • 169
  • 246