Java Import – Is Importing Wildcards a Bad Idea?

importjava

If I do this

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

Then why don't I do this, it's more convenient

import android.graphics.*;

Do you agree that the latter is a risk that I might import classes which conflict which my own (for example "Point" if I had one) and therefore I should not use wildcards in imports. Do you agree?

Best Answer

It is not a bad idea, but it does have some consequences you should be aware of. It's a tradeoff.

Its simpler and shorter, and less programmer overhead typing crap. And its probably less likely to include stray includes that are not needed (though now modern IDEs detect/fix that for you so maybe that doesn't matter).

It CAN result in code that worked fine, when you upgrade the version of your libraries, suddenly stops compiling. But that is insanely unlikely (been doing this for 40 years and I've NEVER seen it happen).

Personally, I try to keep my includes minimal as a form of documentation. For library code (code that's highly leveraged) - its more important to really understand your dependencies. For application code, its a little less important.

No right or wrong - just go for what feels right, IMHO.

Related Topic