Import package.* vs import package.SpecificType

Juan Carlos Blanco Martínez picture Juan Carlos Blanco Martínez · Oct 9, 2008 · Viewed 66.4k times · Source

Would it suppose any difference regarding overhead to write an import loading all the types within one package (import java.*); than just a specific type (i.e. import java.lang.ClassLoader)? Would the second one be a more advisable way to use than the other one?

Answer

Chris Cudmore picture Chris Cudmore · Oct 9, 2008

Take a look at the java API, and you'll see many classes and interfaces with the same name in different packages.

For example:

java.lang.reflect.Array
java.sql.Array

So, if you import java.lang.reflect.* and java.sql.* you'll have a collision on the Array type, and have to fully qualify them in your code.

Importing specific classes instead will save you this hassle.