File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Import statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Import statements" Watch "Import statements" New topic
Author

Import statements

sumanth kadaba
Greenhorn

Joined: Jun 21, 2009
Posts: 27
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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Jun 21, 2009
Posts: 27
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

Joined: Sep 19, 2008
Posts: 385
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.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
sumanth kadaba
Greenhorn

Joined: Jun 21, 2009
Posts: 27
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

Joined: Aug 31, 2004
Posts: 11343

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.


    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    John de Michele
    Rancher

    Joined: Mar 09, 2009
    Posts: 600
    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

    Joined: Oct 27, 2005
    Posts: 19718
        
      20

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    sumanth kadaba
    Greenhorn

    Joined: Jun 21, 2009
    Posts: 27
    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

    Joined: Mar 09, 2009
    Posts: 600
    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

    Joined: Feb 02, 2009
    Posts: 214
    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

    Joined: Aug 16, 2005
    Posts: 14266
        
      21

    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.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Import statements