3

Possible Duplicate:
What is a good use case for static import of methods?

I rarely ever see a static import in java like this:

import static java.lang.Math.*;

Then you could access PI without having to call Math.PI.

Since you don't see this that often, does that mean it is a bad design to do this?

Community
  • 1
  • 1
user489041
  • 27,916
  • 55
  • 135
  • 204

5 Answers5

3

I prefer not to use them, simply because I want to see where each constant is defined. If your classes and your constants are named appropriately, it helps readability a lot.

Then again, if you're using a lot of constants from the same class and it's obvious where they come from, you're better off with a wildcard import.

biziclop
  • 48,926
  • 12
  • 77
  • 104
2

It is not bad design, but in my opinion Math.PI is clearer for maintaneinance than just PI.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
2

Sometimes yes. When you use a static import, the fields and methods from the class you statically imported might "look like" they come from your class.

This does impact understandability, IMHO.

That said, I use it all the time in JUnit tests!

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

It's not bad. It's just usually not necessary. I personally use it whenever my program uses a lot of calls to java.lang.Math.

Most people also don't know about it since it's so infrequently used. Same thing goes to other constructs like static constructors.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
1

Math was around before import static which is why most developers would tend to use the older form.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130