Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why we have to remove unused Import

Reply
Thread Tools

Why we have to remove unused Import

 
 
Joey
Guest
Posts: n/a
 
      07-18-2006
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.

 
Reply With Quote
 
 
 
 
Larry Barowski
Guest
Posts: n/a
 
      07-18-2006

"Joey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-18-2006
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
 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      07-18-2006
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-)
 
Reply With Quote
 
huazonglin@gmail.com
Guest
Posts: n/a
 
      07-18-2006
Thanks everyone, I also want to know does unused import cause memory
problem, I mean, it will use more memory?

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      07-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      07-18-2006
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


 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      07-18-2006
Hi,

(E-Mail Removed) 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

 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      07-18-2006
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

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-18-2006
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
remove unused networks? Limbo Wireless Networking 0 07-02-2005 11:02 AM
Re: Why I can not remove all dialers after "remove"-I have two anti-dialers programs Joseph Ladovic Computer Security 3 05-26-2005 03:00 AM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
cost of unused import statement Jeff Java 13 09-28-2004 05:59 PM



Advertisments