I understand that I can import packages and get access to lots of already coded classes that I can use to make my programs. But if they give you access to so many different features, why not just import them all? I understand that there are thousands of imports and I know it is uncommon to do so (I don't know of anyone that does it but maybe i'm wrong) but why don't people just import them all? Would it make the program too slow? Or just be inefficient? I'm just curious. Thanks.
Asked
Active
Viewed 3,190 times
4
-
2Possible duplicate of [Why is using a wild card with a Java import statement bad?](http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad) – erolkaya84 Sep 05 '16 at 09:16
-
Have you considered the case when you have two classes with the same name, but in different packages? – Adrian Jałoszewski Sep 05 '16 at 09:17
3 Answers
5
There's a few reasons besides speed I can think of:
- Seeing the list of imports can quickly show someone reading the file what is being used. If you import everything, you lose that ability
- There will be name clashes which will cause errors. See this question for an example.
- Modern IDEs make it really easy to import packages on demand, so there's no need to import everything in advance
3
Importing all packages will;
- slow down your program as it keeps all the classes, functions, etc. coming from each package alive (=readily accessible)
- create conflicts between packages which use the same namespace (i.e. same function name etc.), or at least make the last loaded one usable and make the previous ones masked
- take a lot of time every time you restart program
- use a lot of memory
- be vulnerable to crash for the above reasons
where this list can be extended.

Ömer An
- 600
- 5
- 16
-
1imports play no part at runtime and won't have any impact of speed of the program, only at compile time. e.g. it is not possible to reconstruct exactly which imports were used from the byte code. – Peter Lawrey Sep 05 '16 at 09:35
-
1@PeterLawrey that's true for programs which need compilation, but importing all packages will definitely impact on the speed of the interpreted languages like R, Python, PHP etc. – Ömer An Sep 05 '16 at 09:57
-
1
It makes your code unreadable because the person who is reading doesn't see your intentions.ALWAYS REMEMBER: "code is written once, twice maybe more, but hundreds of times read by someone. Example:
In Android SDK there are classes that has the same name but they come from different packages. I think that was GPS location manager or something. Whatever...other programmer could have difficulties, because he needs to think or manually check which import are you using.
I'm not a advanced programmer but I surmise that the output program might be bigger in size.