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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Import or inline?" Watch "Import or inline?" New topic
Author

Import or inline?

Tyler Hanson
Greenhorn

Joined: Dec 31, 2009
Posts: 9
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jul 26, 2007
Posts: 10266
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tyler Hanson
Greenhorn

Joined: Dec 31, 2009
Posts: 9
Thanks for the info guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Import or inline?