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?
4 Answers
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 .

- 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
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.

- 24,429
- 7
- 52
- 49
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.

- 1
- 1

- 10,217
- 9
- 62
- 88
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.

- 9,192
- 4
- 24
- 38