java – Asterisks in import – is it good to use?

Question:

For example, instead

import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.HLayout;

can write

import com.smartgwt.client.widgets.layout.*;

and everything works.

Is it okay to (use '*')?

Is there an additional load on the compiler (it is not felt by the eye), the volume of the connected code (did not investigate), etc. ?

Answer:

An interesting and unpleasant side of importing all classes from a package can be a situation when classes are added to the package:

package a;

import b.*; // тут есть класс B
import c.*; // тут есть класс С

public class A {
    public A() {
        new B();
        new C();
    }
}

The trick will happen if class C is suddenly added to package b – our class will stop compiling. Updated some library and bam – for no reason, no reason. There are also situations when it is not clear from which package the class was imported.

The downside of roll-by-name imports is the large "canvas" of imports themselves – but it is almost always "collapsed" in the editor.

I myself, frankly, never bothered with this – Eclipse imports by name – and let it import itself. It seems to me that the disadvantages of any approach are now well compensated by modern IDEs.

Scroll to Top