File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Import statements

 
sumanth kadaba
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

It's considered a good practice to use imports which are needed.

Say if you want to use File class in the program then you use java.io.File; and not java.io.*

why is it so?
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumanth:

My personal rule of thumb is if there are 1-3 imports from the same package, write each import separately. For 4 or more, use the star notation to make the file visually cleaner.

John.
 
sumanth kadaba
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Thanks for a quick reply. Is
visually cleaner.
the only reason for it being a good practice?

I had assumed that all classes would be loaded by the JVM into the memory while executing the program which is unnecessary.
Is that not the case?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Importing entire package will not load all the classes from the package.

Importing types from the package is used to avoid typing the fully qualified name of the class or interface in each and everyline on the source code.

So when the compiler sees some particular class used inthe source code it will look for the fully qualified name from the package and then load that class and will use it.

So it won't hurt much,even with latest JDK you won't feel any differences at all.
 
sumanth kadaba
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Siva,

Thanks for the reply.

So you mean optimization is done during compile time itself and importing the whole package would be of no cause for concern?

So I was wrong in assuming that all classes would be loaded.

This means according to what John said apart from visual cleanliness there is nothing else to support the statement of it being a good practice.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva is correct. Classes are only loaded when needed, so using a type-import-on-demand declaration (with the asterisk) will not impact performance by loading everything in the package.

Think of import statements as making things "visible." Not loading.

The main benefits to using single-type-import declarations (with canonical names) instead of type-import-on-demand declarations are:
  • Avoiding potential clashes with names. That is, if your code uses a type that happens to have the same name as another type in the package (that you might not even be aware of).
  • Making it clear to other coders what exactly is being used from the import package. That is, why the import statement is there (and whether it's still needed when the code is modified over time).


  • For additional information, see JLS - 7.5 Import Statements.
     
    John de Michele
    Rancher
    Posts: 600
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sumanth:

    The Java compiler will only look for the classes it needs. This is unlike C/C++, where entire library files are included. So, yes, it's simply for visual cleanliness, and has no impact on the number of classes loaded.

    John.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20381
    46
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John de Michele wrote:My personal rule of thumb is if there are 1-3 imports from the same package, write each import separately. For 4 or more, use the star notation to make the file visually cleaner.

    I used to do that as well, but now I (well, Eclipse ) write an import statement for each class I need. That does indeed cause large import lists but at least it's clearer what classes are important, and Eclipse warns me if there is an import that is no longer necessary.

    Sumanth, the best example can be given with two import statements and one declaration:
    Now, which List is meant? java.awt.List or java.util.List? This is the most occurring conflict but there can be others. java.io.FileFilter and javax.swing.filechooser.FileFilter are also two that I've encountered. If you give each class its own import it's immediately clear which one you need.
     
    sumanth kadaba
    Greenhorn
    Posts: 27
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob,

    That was a really good illustration of the topic in question.
    Thanks a lot for that.

    Also thanks to others who made this clear to me.

    Cheers,
    Sumanth
     
    John de Michele
    Rancher
    Posts: 600
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Prime wrote:
    I used to do that as well, but now I (well, Eclipse ) write an import statement for each class I need. That does indeed cause large import lists but at least it's clearer what classes are important, and Eclipse warns me if there is an import that is no longer necessary.


    Yeah, I've pretty much switched over to letting Eclipse do the work for me, too. I got into the habit at a previous job, and now it's just too easy to not even worry about editing the list .

    John.
     
    D. Ogranos
    Ranch Hand
    Posts: 214
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marc weber wrote:Making it clear to other coders what exactly is being used from the import package. That is, why the import statement is there (and whether it's still needed when the code is modified over time).


    I think this is the best reason why you should write explicitely what classes you import.
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Pie
    Posts: 15150
    31
    Android IntelliJ IDE Java Scala Spring
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's not just a question of readable code. Importing classes with * can even make your source code incompatible with a future Java version. So for maximum compatibility, it's better to include only those classes that you actually use.

    Suppose that you import a whole package, for example by using a statement: import java.io.*;

    Now, suppose that you also have a class named Path in your own project. In the future (Java 7 or later), the package java.io might contain a new class named Path. If you used the * to import all classes, you'd suddenly be importing Path from the package java.io instead of your own class Path. Your code suddenly won't compile anymore on the new version of Java.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic