What is the typical convention for importing Java packages?

In my college programming classes, we just imported the whole package, even if we just used one class from it. Perhaps this is due to the fact that we had to use IDEs that did not offer autocomplete or something like that, so just simplify the code as a whole.

However, now that I'm a more experienced programmer, is it more traditional to import Java packages in general or just classes from the packages you need?

For example, instead of:

import java.util.*; 

Is it possible to say:

 import java.util.ArrayList; import java.util.List; import java.util.Random; 

and etc.

Does importing an entire package have more memory than the selected classes?

+4
source share
5 answers

This is not so much a memory problem, but rather the risk of unintentionally importing the wrong class if you use * notation. This is usually not a problem, but if * matches the class that you wanted to extract from another package, this is a difficult mistake to find.

I saw that the IDE uses a rule according to which the import of a certain class is replaced by a single import operator '*' if you import three or more classes from this package. I prefer to call each imported class by name.

+4
source

These are declarations of the type "Wildcard Type - Import On Demand" that you specified as import java.util.*; Should NOT be used directly in code; instead use only the required class name, for example import java.util.ArrayList;

There are the following and many other reasons:

  • An important reason is that a new unexpected class file may be added to the same package. This new class may conflict with the type that you are using from another package, thereby turning the previously correct program into the wrong one without touching the program itself.

  • Explicit class imports explicitly pass on to the reader the exact classes that are being used (and which classes are NOT used).

  • Explicit class imports provide better compilation performance. Although import type declarations on demand are convenient for the programmer and save a little time at first, this time they pay extra compilation time each time the file is compiled.

For more information on coding conventions, see http://geosoft.no/development/javastyle.html#

+3
source

Often, memory consumption will be similar, since the class you import is likely to result from using other classes in the same package due to inheritance logic and functionality.

At my school (junior high school year) they currently force us to import the entire package. This is mainly due to the fact that our IDE (BlueJ) offers a minimum of minimum autocompletion, and therefore it would be very painful to be able to import each individual class that we use.

Personally, I use Eclipse and IDEA for my work and import each class manually. This prevents conflicts between the same name classes in different packages, which helps me a lot, since Android has equivalent classes for many Java frameworks, but in a different package.

+1
source

In terms of performance, there is a small overhead when compiling, but no difference at runtime.

0
source

@ Chris Gerken has a good point.

But these are not just mistakes. Importing from Wild Card can lead to problems if you recompile the updated library, which (in the new version) adds a new class with the same name that you already use. Thus, importing wild cards tends to make your code more fragile in the long run.

0
source

Source: https://habr.com/ru/post/1499891/


All Articles