![]() |
|
|
|||||||
![]() |
Java - Why we have to remove unused Import |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
"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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
Thanks everyone, I also want to know does unused import cause memory
problem, I mean, it will use more memory? huazonglin@gmail.com |
|
|
|
#6 |
|
Posts: n/a
|
|
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |