• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

imports

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I generally switch to * once the number of classes imported from a package exceeds 3.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic