• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

imports

 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2745
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generally switch to * once the number of classes imported from a package exceeds 3.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm, yeah. Although I assume that other IDEs have similar functionality, possibly with a different key combination.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic