Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upsides and downsides of importing specifically and importing overally

Tags:

java

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?

like image 732
siaooo Avatar asked Jun 21 '11 06:06

siaooo


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

like image 109
amal Avatar answered Oct 05 '22 12:10

amal


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.

like image 36
Alex Gitelman Avatar answered Oct 05 '22 13:10

Alex Gitelman