Style question: When to import using wildcards (somepackage.*)?

I have a style question: When is it appropriate to use the wildcard asterix when importing packages and libraries?

My gut instinct is that one should generally import each class as needed, but that using the wildcard is a quick shortcut used when there are going to be too many import statements. But am I right in thinking that there's a price to pay in terms of either speed or just the size of the final program if wildcards are used?

For example, my gui (created by Netbean's gui-builder) has the import statements

import java.awt.Component;

import java.awt.Dimension;

import java.awt.DisplayMode;

import java.awt.GraphicsDevice;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;

import java.awt.Color;

import java.awt.Toolkit;

import java.awt.Point;

import java.awt.Cursor;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.ImageIcon;

import java.net.URL;

import java.io.File;

My guess is that most programers, if they were writing the gui by hand, would have used

import java.awt.*

am I right? But is the former better and the latter a bad habit?

Also, I assume that, if I know I'm going to want the entire package, I should use the wildcard? For instance, in one of my programs, I have a set of data types in a package called 'datatype'. As I use them all in one of my classes, I use 'Import datatype.*'. Is there any performance benefit either way in this case?

Thanks!

--Sam

[2323 byte] By [Asbestosa] at [2007-10-3 1:31:16]
# 1

There's no price in terms of either speed or the size of the final program if wildcards are used; it makes absolutely no difference in the compiled bytecode. The only potential impact is at compile time, but that's likely to be negligible at best.

As a general rule of thumb, I use the wildcard if my imports exceed 3-5 imports from the same package (usually 3, if I'm using a text editor; 5 if I can set the imports in the IDE).

~

yawmarka at 2007-7-14 18:29:10 > top of Java-index,Java Essentials,New To Java...
# 2

Really the best answer to this question is: Readablility (to some extent)

This question was just asked a couple days ago and that was the answer given then (and most other times) and for good reason.

When I look at your code and see a .* wildcard I have to hunt through your program to see what you actually used from that package.

I always use the wildcard for .awt and .swing packages.

Obviously common sense should be at play. If its a GUI class a wildcard for awt and swing is reasonable - as writing out a header of 100 classes would be awful.

A recent real world experience highlights this. I had to reverse engineer

a project for the company i work for and me and another engineer were

getting lost in a sea of wildcards and company packages. We needed to know the company classes that were being used so we had to weed

through the code for them.

If the original programmer had been more considerate he would have

just written out the 1 or 2 classes he used.

TuringPesta at 2007-7-14 18:29:10 > top of Java-index,Java Essentials,New To Java...
# 3

More succinctly, id say its an issue of the level of package familiarity

youd think future programmers would have.

Like i said, with most GUI code i just wild card awt and swing and their

events but with company packages i try to be very clear about what im using.

People wont mind: java.util.regex... id guess Matcher and Pattern are at play

This however: com.my.company.mygroup.project.obscure.everyclass.*;

TuringPesta at 2007-7-14 18:29:10 > top of Java-index,Java Essentials,New To Java...
# 4
In case you don't know some of the implications of using a wildcard vs. not using one, see Reply #2 here: http://forum.java.sun.com/thread.jspa?threadID=658399
MLRona at 2007-7-14 18:29:10 > top of Java-index,Java Essentials,New To Java...
# 5
Ok, thanks to all for your advice!
Asbestosa at 2007-7-14 18:29:10 > top of Java-index,Java Essentials,New To Java...