Import multiple classes from the same package

Asked

Viewed 941 times

8

I wonder if there’s 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, the classloader will load all classes of the package java.util in JVM even if I’m not going to use them? It will influence memory performance if I abuse the use of wildcards? Finally, what is good practice?

Related

  • 1

    The second option, the correct one is to import the class you will use directly, unless you are going to use all the classes of a very extensive package, then the call with the joker compensates more, but I can’t imagine a scenario where this will occur.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

7

Regarding memory there is no difference, the instruction import serves only to tell where the classes used are.

But there is an advantage in using it directly importing the class. If there are classes with the same name in two packages a conflict will occur in which class is being referenced.

For example, using the packages javax.swing.text and org.w3c.dom, whereas the two have the class Element:

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

...

Element elemento;

In this case the Element used is which package?

Already in the following example:

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

...

Element elemento;

We know so that the Element used is from the package org.w3c.dom. So in reality there is an advantage in spelling out the imported classes in the context of facilitating the reading and understanding of the code. Not that it matters * whether bad or wrong, only facilitates future maintenance.

Import all classes from a package with * is justified only by the ease of not having to change imports manually for each new class used. To facilitate this aspect and to avoid it being necessary to have knowledge of the exact path to the package that will be used, the main IDEs developmental 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 setting):

  • Netbeans: Ctrl + Shift + I
  • Eclipse: Ctrl + Shift + The
  • Intellij: Ctrl + Alt + The

Atalho - Netbeans


Answer references for similar questions on Stack Overflow:

6

Not unless it really matters, even something you might not want to matter. Nothing prevents you from having name conflicts in the classes that are imported and you might not even notice using them. Using the name explicitly you select best what will be imported.

The use of * 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 individually it is clear that it will not be imported if you do not change your code. What should not be important because surely your code does not make use of it.

Of course it matters everything that is on 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 an additional cost virtually imperceptible in the compilation because there may be more places for it to search for symbols used in its code. But it will be no more than the time elapsed if you had put all packets one by one in hand. The difference would be only by the amount.

The difference is the intention. You want to import everything, or a list of packages, even if they happen to be all?

  • The most internal packages I think are not imported, there have been several cases where I used classes in subpacks of a particular package with the joker * and java has forced to import the entire subpackage or class within that subpackage.

  • 1

    @diegofm ah yes, you have to do by level. The * is not *.*.*.* :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.