6

I noticed that people prefer importing javax.swing.JFrame instead of javax.swing.* , importing java.math.BigInteger instead of java.math.* , etc... Is there any downside of importing the whole package instead of importing specifically or is there any upsides importing specifically?

dacwe
  • 43,066
  • 12
  • 116
  • 140
siaooo
  • 1,827
  • 3
  • 23
  • 25

4 Answers4

3

Well one point I've have read against package imports is that they cause problems if the classes are added to the package later causing ambiguity . Like jdk 1.1 contained just one List class in java.awt package , jdk1.2 introduced another List class in java.util package .

amal
  • 1,369
  • 8
  • 15
  • +1 That's the main point. It can break existing code. You update your JDK, try to compile/build your project and get tons of errors because of that. Sure, it may be relatively unlikely but like your example shows it's actually a real problem. – helpermethod Jun 21 '11 at 06:58
2

Usually single class imports are preferred because they make it easy to figure which class is imported. And with modern IDE it's very easy. So it's often considered a good style. There is no difference between package and single class imports.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
1

None whatsoever. The import statement is a compiler directive and has no effect on the compiled code. You can have a further read here and here (you can probably find better sources, but that was a 1 minute Google exercise).


This also seems like a duplicate of this question.

Community
  • 1
  • 1
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
0

You might notice a slower compilation time when you do wildcard import, this is because all classes would be loaded by the compiler at compile time. But it has no effect to the runtime performance.

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38