This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Import or inline?

 
Tyler Hanson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a general question regarding imports. Should one use the import statement or just write the packages inline? I myself have adopted a strategy where I will never import using a wildcard and will only import classes if I use them only once. If I use them twice, i will typically import it. Or does it really not matter? I find most of the time I am writing the packages inline, but I was thinking that maybe this is and an incorrect concept.

Thanks,
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I don't import using wildcard. I import only the classes I need. I don't know that I have ever used inline as opposed to import, though. Maybe I am wrong, but my thinking is I would rather import now, even if I only use it once, rather than come back later and need to import if I use the class multiple times.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason that it matters which you do is whether it's more understandable to the programmer to do it one way rather than the other way. So if you find that you have problems understanding this:

because you didn't remember that Projector was in the com.bananarama package, and that was a source of confusion, then by all means change it to read

and take out the include statement.

I think you'll find that the vast majority of programmers prefer the first form, along with a suitable include statement, rather than the second form, but it's up to you which you actually use.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason I ever had to use inline import statement was in a case where I my code used two different classes with the same name but from multiple packages (one standard java class and one from a third party API in my case) Else I prefer to import the classes using import statements.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always import only the classes that I need, and I don't use the wildcard to import classes. In fact, I let my IDE organise the imports for me. Both Eclipse and NetBeans have the key combination Ctrl + Shift + I to automatically organise the imports.

Using the wildcard with imports introduces a potential future compatibility problem in your code. Suppose that you do import java.util.*; in your code, and that one of your own classes is named Something. Now, a new version of Java comes out, in which there is a new class in the java.util package that also happens to be called Something. If you imported everything from java.util with the wildcard, the new Something from java.util would also be imported. You'll get compiler errors when you'd use that new version of Java, because the Java compiler can't distinguish between your class and the new class in java.util.

One example where you might need to use the fully qualified names of classes are the java.util.Date and java.sql.Date classes. Sometimes you need to use both those Date classes in the same source file, and to distinguish them you need to name them with their package names prefixed.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:I Both Eclipse and NetBeans have the key combination Ctrl + Shift + I to automatically organise the imports.

On Eclipse it is Ctrl+Shift+O for me which is the default (I think)
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Jesper Young wrote:I Both Eclipse and NetBeans have the key combination Ctrl + Shift + I to automatically organise the imports.

On Eclipse it is Ctrl+Shift+O for me which is the default (I think)

It is.

I agree with both Maneesh and Jesper. I import only what I need (except in quick test classes), and only use inline imports in case of conflicts; e.g. using both java.io.FileFilter and javax.swing.filechooser.FileFilter in one class. In those cases I always inline import the shortest
 
Tyler Hanson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info guys!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic