What is the best way to import multiple classes from the same package. The easiest is to import the entire package, although classes that are not needed can be imported here.
import mipaquete.foo.*
The other way is longer but it would only matter what you need:
import mipaquete.foo.Clase1
import mipaquete.foo.Clase2
// Otras clases importadas aquí.
import mipaquete.foo.Clase5
Does it affect performance when the entire package is imported with classes that are not used?
The problem could arise when referencing the entire contents of the package (using *), when you have classes with the same name that exist in different packages, this could cause a problem which would not allow compiling.
Solving this problem requires precisely that any ambiguous class or interface name be fully specified.
There is actually no performance hit specifying the import of the classes in the package versus importing a specified class in the package, since the import is related to compilation only.
It is important to note that in the language
Java
, the use ofimport
does not produce "code bloat" , since it does not actually include unused classes in the application.It is certainly good practice to always specify the classes used in your project to avoid ambiguity, avoiding importing all types contained in a package using the import statement with the asterisk (*) wildcard character.
I recommend importing class by class. By importing all with * , you are not penalizing the performance of the application (the class loader loads the entire .jar) but you are increasing the size of the class unnecessarily and can penalize code reading/maintenance.