In terms of memory there is no difference, the import
statement is just to say where the classes used are.
But there is an advantage in using it by directly importing the class. If there are classes with the same name in two packages there will be a conflict regarding which class is being referenced.
For example, using the javax.swing.text
and org.w3c.dom
packages, considering that both have class Element
:
import javax.swing.*;
import org.w3c.dom.*;
...
Element elemento;
In this case, what% of the% used is which package?
In the following example:
import javax.swing.text.Document;
import org.w3c.dom.Element;
...
Element elemento;
We now know that the% wc% used is from the Element
package. So in reality there is an advantage in making explicit the imported classes in order to facilitate the reading and understanding of the code. Not that importing with Element
is bad or wrong, it only facilitates future maintenance.
Importing all classes from a package with org.w3c.dom
is justified only by the ease of not having to change imports manually for each new class used. To make it easier in this respect and to avoid having to know the exact path to the package that will be used, the main *
development% have shortcuts to optimize imports, removing those that are not being used, and presenting package options for which they are not declared. Some are listed below (In the default configuration):
-
Netbeans: Ctrl + Shift
-
Eclipse: Ctrl + Shift +
-
IntelliJ: Ctrl + Alt
Responsereferencestosimilarquestionsin Stack Overflow :