wood burning stoves 2.0*
The moose likes Java in General and the fly likes imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "imports" Watch "imports" New topic
Author

imports

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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.


SCJP
Visit my download page
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

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.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
When you try to static import a class holding all constants use *

import static com.mypackage.data.MyApplicationConstants.*;

MyApplicationConstants is a class here
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I generally switch to * once the number of classes imported from a package exceeds 3.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

I think with specific imports the readability of the code also improves. One can identify the classes involved by looking at the imports. Wider imports are at time ambiguous.


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

thanks for the info and opinions. in the past i usually started like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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.
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

Rob Spoor wrote:I just have to press Ctrl+Shift+O and all imports are corrected, including the removal of unused imports.


"Eclipse"!! Of course it was a no brainier after you look at Rob's bumper stickers

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Uhm, yeah. Although I assume that other IDEs have similar functionality, possibly with a different key combination.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

call me "old school" but i have never really liked IDE's

it is probably because the first one i used, Forte, took about 10 minutes to load(run) on my Pentium 1
i didn't like the full class names for everything either
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: imports
 
Similar Threads
Static imports
Why these imports in AdviceClient class?
questions on import
importing from same name packages
IntelliJ 4.0: My favorite shortcuts