Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Why we have to remove unused Import

 
Thread Tools Search this Thread
Old 07-18-2006, 02:46 AM   #1
Default Why we have to remove unused Import


Hello Everyone.

I want to know why we have to remove those unused Import, use
java.util.ArrayList, don't use java.util.*, why we have to do this.
just for good code style ? or ....


Thanks.

Joey.



Joey
  Reply With Quote
Old 07-18-2006, 05:40 AM   #2
Larry Barowski
 
Posts: n/a
Default Re: Why we have to remove unused Import

"Joey" <> wrote in message
news: ups.com...
> Hello Everyone.
>
> I want to know why we have to remove those unused Import, use
> java.util.ArrayList, don't use java.util.*, why we have to do this.
> just for good code style ? or ....


Using package imports can cause hidden problems. When
you upgrade Java, or some other library, there may be
new classes. The worst case is that you have a class with
the same name as one of these, that has all the methods
you use, and that does almost the same thing or is rarely
used and rarely tested. So your project will compile, but
will break at runtime in rare circumstances. For a large
project, this is not as improbable as you might think. A
more common case is that a new class with the same
name as one you are using is introduced, but you are
using methods it does not have, or it breaks in an
immediate and obvious way at runtime.




Larry Barowski
  Reply With Quote
Old 07-18-2006, 05:47 AM   #3
Patricia Shanahan
 
Posts: n/a
Default Re: Why we have to remove unused Import
Joey wrote:
> Hello Everyone.
>
> I want to know why we have to remove those unused Import, use
> java.util.ArrayList, don't use java.util.*, why we have to do this.
> just for good code style ? or ....


You usually don't HAVE to do this.

The only case where importing too much is an error is when two package
imports both cover the same name. The classic example is java.util.* and
java.awt.*, which both have define "List".

However, there are two motivations for keeping imports pruned:

1. Avoiding future ambiguity. Recompiling with a new version of the SDK
could introduce an ambiguity at an inconvenient time.

2. Conveying information to readers of the program. Seeing the actual
imported class list gives an impression of the sort of things the
program does.

Patricia


Patricia Shanahan
  Reply With Quote
Old 07-18-2006, 06:13 AM   #4
IchBin
 
Posts: n/a
Default Re: Why we have to remove unused Import
Patricia Shanahan wrote:
> Joey wrote:
>> Hello Everyone.
>>
>> I want to know why we have to remove those unused Import, use
>> java.util.ArrayList, don't use java.util.*, why we have to do this.
>> just for good code style ? or ....

>
> You usually don't HAVE to do this.
>
> The only case where importing too much is an error is when two package
> imports both cover the same name. The classic example is java.util.* and
> java.awt.*, which both have define "List".
>
> However, there are two motivations for keeping imports pruned:
>
> 1. Avoiding future ambiguity. Recompiling with a new version of the SDK
> could introduce an ambiguity at an inconvenient time.
>
> 2. Conveying information to readers of the program. Seeing the actual
> imported class list gives an impression of the sort of things the
> program does.
>
> Patricia


IMHO - Also, if say I pick up your program, after you have left the
company, and look at the imports I can get a good feel on what to
expect from the program.

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)


IchBin
  Reply With Quote
Old 07-18-2006, 06:42 AM   #5
huazonglin@gmail.com
 
Posts: n/a
Default Re: Why we have to remove unused Import
Thanks everyone, I also want to know does unused import cause memory
problem, I mean, it will use more memory?



huazonglin@gmail.com
  Reply With Quote
Old 07-18-2006, 08:20 AM   #6
Chris Uppal
 
Posts: n/a
Default Re: Why we have to remove unused Import
wrote:

> Thanks everyone, I also want to know does unused import cause memory
> problem, I mean, it will use more memory?


It might take a little more memory at compile-time (but why should you care ?).
At runtime it makes exactly, mathematically, zero difference.

-- chris




Chris Uppal
  Reply With Quote
Old 07-18-2006, 08:23 AM   #7
Chris Uppal
 
Posts: n/a
Default Re: Why we have to remove unused Import
IchBin wrote:

> IMHO - Also, if say I pick up your program, after you have left the
> company, and look at the imports I can get a good feel on what to
> expect from the program.


Personally, I consider the long lists of not-obviously related explicit imports
that tools like Eclipse can cow you into creating to be /less/ clear than a few
wildcard imports.

And if the risk of a sudden "new" ambiguity is something that seriously bothers
you ("you" not meant personally, of course) then there is something very
seriously wrong with your project management.

-- chris




Chris Uppal
  Reply With Quote
Old 07-18-2006, 08:27 AM   #8
Ingo R. Homann
 
Posts: n/a
Default Re: Why we have to remove unused Import
Hi,

wrote:
> Thanks everyone, I also want to know does unused import cause memory
> problem, I mean, it will use more memory?


No.

AFAIK, from the compiled class-file, you cannot see, if there were
unused imports, because the compiler "removes" (*) them.

Ciao,
Ingo

(*) For pedantic persons, "remove" is not the correct word in this case,
but that does not matter



Ingo R. Homann
  Reply With Quote
Old 07-18-2006, 08:30 AM   #9
Ingo R. Homann
 
Posts: n/a
Default Re: Why we have to remove unused Import
Hi,

Larry Barowski wrote:
>>I want to know why we have to remove those unused Import, use
>>java.util.ArrayList, don't use java.util.*, why we have to do this.
>>just for good code style ? or ....

>
>
> Using package imports can cause hidden problems. When
> you upgrade Java, or some other library, there may be
> new classes. The worst case is that you have a class with
> the same name as one of these, that has all the methods
> you use, and that does almost the same thing or is rarely
> used and rarely tested. So your project will compile, but
> will break at runtime in rare circumstances. ...A
> more common case is that a new class with the same
> name as one you are using is introduced, but you are
> using methods it does not have, or it breaks in an
> immediate and obvious way at runtime.


Eh... just a second...:

Joey asked for *unused* imports. If they are unused, the problems you
mention cannot occur!

Ciao,
Ingo



Ingo R. Homann
  Reply With Quote
Old 07-18-2006, 10:23 AM   #10
Patricia Shanahan
 
Posts: n/a
Default Re: Why we have to remove unused Import
wrote:
> Thanks everyone, I also want to know does unused import cause memory
> problem, I mean, it will use more memory?
>


No difference at run time. All the imports do is tell the compiler how
to calculate the fully qualified class name, such as "java.util.List",
from an unqualified name such as "List".

Patricia


Patricia Shanahan
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DVDV The newer DVD type bobs.auto.service@coldmail.com DVD Video 0 01-02-2006 03:56 PM
DIABOLIK DVD: Just Arrived Import/Rare Discs! Joseph A. Gervasi \(Diabolik DVD/Exhumed Films\) DVD Video 0 10-25-2004 03:51 PM
DIABOLIK DVD: A Partial List of New Titles (Import, Rare) Joseph A. Gervasi \(Diabolik DVD/Exhumed Films\) DVD Video 0 10-12-2004 07:56 PM
DIABOLIK DVD: New Import & Rare Discs! Joseph A. Gervasi \(Exhumed Films/Diabolik DVD\) DVD Video 0 01-20-2004 01:17 AM
Latest Tech Fiasco... Ghost A+ Certification 30 01-09-2004 12:15 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46