Import multiple classes from the same package

I would like to know if there is any significant difference in making

import java.util.*

Instead of

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

My question is whether I am "overloading" the classloader if I use the wildcard instead of a list with the specific classes. That is, will the classloader load all classes from the java.util package into the JVM even if I'm not going to use them? Will it influence memory performance if I abuse the use of wildcards ? Finally, what is good practice?

Related

Author: Comunidade, 2017-02-08

2 answers

In relation to memory there is no difference, the statement import serves only to tell where the classes used are.

But there is an advantage to using 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 packages javax.swing.text and org.w3c.dom, whereas both have the Class Element:

import javax.swing.*;
import org.w3c.dom.*;

...

Element elemento;

In this case the Element used is of which package?

Already in the following example:

import javax.swing.text.Document;
import org.w3c.dom.Element;

...

Element elemento;

We know that the Element used is from the org.w3c.dom package. So in reality there is an advantage in explaining the imported classes in the scope of making it easier to read and understand the code. Not that importing with * is bad or wrong, it just facilitates future maintenance.

Importing all classes from a package with * is justified only by the ease of not having to change the imports manually for each new class used. To facilitate this aspect and avoid having to know the exact path to the package that will be used, the main IDEs development Java have shortcuts to optimize imports, removing those that are not being used and presenting package options for those that are not declared. Some are listed below (in the default configuration):

  • Netbeans: Ctrl + Shift + I
  • Eclipse: Ctrl + Shift + O
  • IntelliJ: Ctrl + Alt + O

Shortcut-Netbeans


Answer references for similar questions in Stack Overflow:

 7
Author: Sorack, 2017-05-23 12:37:31

No, unless everything matters, even something you might not want to matter. Nothing prevents you from having name conflicts in the classes you import,and you may not even notice them. Using the name explicitly you better select what will be imported.

Using * ensures that everything will be imported, even what does not yet exist. If you create a new class in the future within the util with the asterisk It will be imported, if you do it individually of course you do not it will be imported if you do not change your code. Which shouldn't matter because surely your code doesn't make use of it.

Of course it imports everything that is at that package level, it doesn't do it recursively.

There is no performance loss on application load. At least not in the code. There may be a virtually unnoticeable additional cost on compilation because it may have more places for it to look for symbols used in your code. But it will be no more than time elapsed if I had put all packages one by one in hand. The difference would be only by quantity.

The difference is the intention itself. Do you want to import everything even, or a list of packages, even if they are, by chance, all?

 6
Author: Maniero, 2017-02-09 12:26:21