Import multiple classes from the same package

7

I wonder if there is any significant difference in doing

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'm "overloading" the classloader if I use the wildcard instead of a list with specific classes. That is, will the classloader load all classes of the java.util package in the JVM even if I am not going to use them? Will it influence memory performance if I abuse the wildcards ? Finally, what is good practice?

Related

    
asked by anonymous 08.02.2017 / 13:04

2 answers

6

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 :

08.02.2017 / 13:39
5

No, unless you import everything yourself, even something you may not want it to matter. Nothing prevents you from having name conflicts in the classes you import and you may not even realize it until you use them. By 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 util with the asterisk it will be imported, if you do it of course it will not be imported if you do not change your code. Which should not be important because your code certainly does not make use of it.

Of course, it imports everything in that package level, it does not recursively.

There is no loss of performance in the application load. At least not in the code. There may be a virtual extra cost in the build because you may have more places to look for symbols used in your code. But it will not be more than the time elapsed if he had placed all packages one by one in his hand. The difference would be only by quantity.

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

    
08.02.2017 / 13:36