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,066
  • 55
  • 130
  • 201

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
  • 47,775
  • 12
  • 76
  • 101
2

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

Oscar Gomez
  • 18,248
  • 12
  • 81
  • 116
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
  • 82,964
  • 16
  • 166
  • 221
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
  • 34,979
  • 14
  • 68
  • 136
1

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

Peter Lawrey
  • 513,304
  • 74
  • 731
  • 1,106