Java: asterisk for loading classes

Loads only the needed classes directly in a good way to reduce the overall memory usage of a Java application?

For instance:

import java.awt.Graphics; 

against

 import java.awt.*; 
+4
source share
6 answers

Not. You should only import the necessary classes to make it clear to the programmers which classes your class really needs.

The import statements simply tell the compiler where to look for the classes that you use - this does not mean that all classes in the package are loaded into memory.

+16
source

Simply put: no.

Operators

import not translated into any form of bytecode. These are just shortcuts to avoid using (ugly!) Fully qualified type names.

+6
source

As others have expressed, imports are used only by the compiler. You CAN write your entire program without importing using the full names of everything, but it will quickly grow quite large.

 java.io.InputStream is = new java.io.FileInputStream(new java.io.File("foo")); 

Star imports should make it less tedious to write all import instructions manually, but lead to too many things to import, so the compiler has several options. Thus, modern IDEs such as Eclipse import everything one at a time, so this cannot be.

+4
source

You mean star import like import pack.*; ?

In Java, it has nothing to do with memory usage; import is only used to change how you refer to classes. However, there are coding issues regarding the import of stars.

+1
source

I usually use fully qualified type names, rather than such large statements. Packages exist for some reason, and this is because the developer of this package most likely populated the package namespace with all kinds of crap that you don't like, and you just want this class.

+1
source

Explicitly importing the classes you need and doing * import doesn't matter. The JVM only downloads what you end up using, and nothing more.

Having said that explicitly importing your classes is a good solution for software development (and * import is a sign of weak people who don't know or don't care about good software development principles ... I'm not joking, I'm dead seriously.)

0
source

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


All Articles