i know this is a matter of opinion, but when to use *?
is 4 imports from a package enough to use *?
some people seem to want to list every single import explicitly. it helps the readability a little(i guess) unless you have dozens of them.
I'd always say explicitly import the class you need. Writing an import '*' leads to issue when a same name class becomes available in other package. (java.sql.date & java.util.date). Usually imports get out of the way in IDE & never bother you once the required classes are imported.
I refuse to use * in non-test classes. There is just too much ambiguity with certain class names; java.util.Date vs java.sql.Date, java.io.FileFilter vs javax.swing.filechooser.FileFilter and java.util.List vs java.awt.List are just a few examples I've encountered numerous times.
There's a subtle future compatibility problem with using the * syntax.
Suppose you import java.util.* and you're also using a class named SomeClass (doesn't matter from which package that class comes). Then, suppose that in the next version of Java, there is a new class in the java.util package that by chance happens to be called SomeClass as well.
Now your code suddenly won't compile anymore on that new version of Java, because there's an ambiguity between your own SomeClass and the new java.util.SomeClass (which you are importing implicitly through the import java.util.*).
If you would have imported only those classes that you're really using from java.util (instead of importing java.util.*) you would not have had that problem.
thanks for the info and opinions. in the past i usually started like this:
then made explicit imports from other packages.
i think from now on i will only use * for the swing stuff. i mean all the classes start with J so not much chance of ambiguity. and i usually use a lot of them.
I think one thing that really helps is IDE support. If I had to enter all the imports myself I'd use *, and stuff all the (good) arguments as to why it's a bad idea. But a modern IDE will enter the import for me when I use a new class. And it lets me collapse the list of imports if they get in the way.
I tend to do fully qualified import statements as well - as stated, tools such as Eclipse makes it easy. Also leaning on Eclipse, I try to keep the import statements 'clean' by deleting off the imports which are no longer referenced in the class.
Matthew Brown wrote:I think one thing that really helps is IDE support. If I had to enter all the imports myself I'd use *, and stuff all the (good) arguments as to why it's a bad idea. But a modern IDE will enter the import for me when I use a new class. And it lets me collapse the list of imports if they get in the way.
Plus you can configure most IDEs to automatically use * after the number of imports from one package exceeds a configurable threshold. I set my threshold to 99 or some other high value. I just have to press Ctrl+Shift+O and all imports are corrected, including the removal of unused imports.