Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2009

Re: Why so many imports instead of java.io.* ?

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Why so many imports instead of java.io.* ?
Date 2012-08-14 16:31 -0700
Organization http://groups.google.com
Message-ID <4772cee5-44de-4064-ac8c-389ce431b0ce@googlegroups.com> (permalink)
References <502a4326$0$295$14726298@news.sunsite.dk>

Show all headers | View raw


Timothy Madden wrote:
> Is it just me or people tend to enumerate all the needed classes for a
> Java source file each in its own import line ?

There is a strong indication that import-on-demand is not as good a 
practice as single-type imports.

<http://javadude.com/articles/importondemandisevil.html>
<http://www.javabestpractice.com/java/disadvantage-of-using-wildcards-in-java-import-statements.html>

"Using single-type imports is quite useful and an absolute requirement in the open source community as this code is usually really reviewed. Using single-type imports makes it easy for the code reader to quickly find out the package a particular type is in: You just search for the type name from the start of the source file."
<http://jalopy.sourceforge.net/existing/imports.html>

For arguments on both sides:
<http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad>

And in general, why not do a search yourself across the InterWebz?
<http://lmgtfy.com/?q=java+import+on+demand+vs.+single-type+import>

> Why is it better to use: 
>    import java.io.IOException
>    import java.io.FileNotFoundException
>    import java.io.FileOutputStream
>    import ...
> 
> instead of just:
>    import java.io.*
> ?

Less confusion, elimination of simple-name collisions, more explicit 
documentation of what types it uses.

> Which, by the way, also needs no maintenance in case a new class or
> exception is needed in the file at a later time.

Not true.

It will need maintenance if that type name conflicts with another.

You don't actually need any import directives in a program at all.

The maintenance required for single-type import is negligible and not 
worth avoiding.

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why so many imports instead of java.io.* ? Timothy Madden <terminatorul@gmail.com> - 2012-08-14 15:23 +0300
  Re: Why so many imports instead of java.io.* ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-14 08:45 -0400
    Re: Why so many imports instead of java.io.* ? Patricia Shanahan <pats@acm.org> - 2012-08-14 06:16 -0700
      Re: Why so many imports instead of java.io.* ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-14 10:52 -0400
  Re: Why so many imports instead of java.io.* ? Jeff Higgins <jeff@invalid.invalid> - 2012-08-14 09:05 -0400
  Re: Why so many imports instead of java.io.* ? Lew <lewbloch@gmail.com> - 2012-08-14 16:31 -0700
    Re: Why so many imports instead of java.io.* ? Timothy Madden <terminatorul@gmail.com> - 2012-08-16 16:39 +0300
      Re: Why so many imports instead of java.io.* ? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-16 11:51 -0400
        Re: Why so many imports instead of java.io.* ? Timothy Madden <terminatorul@gmail.com> - 2012-08-17 15:08 +0300
          Re: Why so many imports instead of java.io.* ? Lew <lewbloch@gmail.com> - 2012-08-17 11:26 -0700
            Re: Why so many imports instead of java.io.* ? Gene Wirchenko <genew@ocis.net> - 2012-08-17 12:53 -0700
              Re: Why so many imports instead of java.io.* ? Lew <lewbloch@gmail.com> - 2012-08-17 12:58 -0700
                Re: Why so many imports instead of java.io.* ? Gene Wirchenko <genew@ocis.net> - 2012-08-17 13:27 -0700
                Re: Why so many imports instead of java.io.* ? Timothy Madden <terminatorul@gmail.com> - 2012-08-20 19:09 +0300
                Re: Why so many imports instead of java.io.* ? Patricia Shanahan <pats@acm.org> - 2012-08-20 09:40 -0700
                Re: Why so many imports instead of java.io.* ? Timothy Madden <terminatorul@gmail.com> - 2012-08-21 15:03 +0300
                Re: Why so many imports instead of java.io.* ? Gene Wirchenko <genew@ocis.net> - 2012-08-21 09:05 -0700
                Re: Why so many imports instead of java.io.* ? Patricia Shanahan <pats@acm.org> - 2012-08-21 09:48 -0700
                Re: Why so many imports instead of java.io.* ? Lew <lewbloch@gmail.com> - 2012-08-21 11:51 -0700
                Re: Why so many imports instead of java.io.* ? Patricia Shanahan <pats@acm.org> - 2012-08-17 14:39 -0700
                Re: Why so many imports instead of java.io.* ? Lew <lewbloch@gmail.com> - 2012-08-17 15:39 -0700
                Re: Why so many imports instead of java.io.* ? Gene Wirchenko <genew@ocis.net> - 2012-08-17 15:57 -0700
                Re: Why so many imports instead of java.io.* ? Lew <noone@lewscanon.com> - 2012-08-18 11:03 -0700
                Re: Why so many imports instead of java.io.* ? Gene Wirchenko <genew@ocis.net> - 2012-08-19 18:07 -0700
  Re: Why so many imports instead of java.io.* ? Roedy Green <see_website@mindprod.com.invalid> - 2012-08-14 17:46 -0700

csiph-web