For Java, can I import all packages at once?

后端 未结 6 995
不思量自难忘°
不思量自难忘° 2021-01-18 07:15

For example, the third party library uses very complex directory structure for the package. Can I import them at once. the star seems can only imports one directory level.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 07:47

    I strongly suggest, as others have done, that you use the "organize imports" from your IDE (most major java IDE will do this).

    Also, I advice against using "*" in your imports, and the reason for this is simple. At some time, you might want to upgrade one of the library that you import (or the jdk) and you might end up with name clashes due to some new classes in a package (this is especially true for the static imports). It might not be a big deal if that happens to a class inside your current project, but if you make that code into a library at some point, it might be more problematic as you will have to recompile that class after adjusting the conflicting imports.

    (Yes, that happened to me not so long ago... It was a pain as I had to hunt down the library source code to rebuild.)

    Save yourself some trouble down the road. :D

提交回复
热议问题