![]() |
single package import v/s the entire package
what is the advantage of importing a single class v/s all the classes
from the package like java.util.date is better then java.util.*.If yes then what are the advantages ? ~Parvinder |
Re: single package import v/s the entire package
Parvinder wrote:
> what is the advantage of importing a single class v/s all the classes > from the package > like java.util.date is better then java.util.*.If yes then what are the > advantages ? The main advantage is that if you're unsure what package a class you're using belongs to, you just have to look at the import statements. Also, it will speed up compilation a bit. |
Re: single package import v/s the entire package
Parvinder wrote:
> what is the advantage of importing a single class v/s all the classes > from the package > like java.util.date is better then java.util.*.If yes then what are the > advantages ? I'm surprised I couldn't find this in Roedy's Java Glossary. The different import statements have no effect at runtime, since all code explicitly references the full path of a class. 1. Importing a class explicitly helps to point out exactly where a class comes from. 2. Listing too many classes makes for a very cluttered list in your code. 1 & 2 can be balanced using various schemes including: a. import x.* from 'standard' packages only, where what is standard may be project specific b. import x.* when the number of classes from one package exceeds some threshold, maybe 5 to 10. b is support by Eclipse. -Paul |
Re: single package import v/s the entire package
> Parvinder wrote:
> > > what is the advantage of importing a single class v/s all the classes > > from the package > > like java.util.date is better then java.util.*.If yes then what are the > > advantages ? Michael Borgwardt <brazil@brazils-animeland.de> writes: > The main advantage is that if you're unsure what package a class you're > using belongs to, you just have to look at the import statements. > > Also, it will speed up compilation a bit. And also, if you upgrade a package, or are not aware of the full class list that belongs to the package, then you will avoid any potential name clashes that a exhaustive import may bring. Cheers pete |
Re: single package import v/s the entire package
Parvinder wrote:
> what is the advantage of importing a single class v/s all the classes > from the package > like java.util.date is better then java.util.*.If yes then what are the > advantages ? The advantage of importing a single class is that it avoids naming conflicts that sometimes arise when the same class name is used in more than one package. Example import java.awt.* import java.util.* List list = new ArrayList (); Which List is meant? Both java.awt and Java.util think they know what "List" means (in java.awt it is a class, in java.util it is an interface). The compiler will stop and demand clarification. The solution to this is to add this import after the two above: import java.util.List; This has the effect of forcing any references to "List" to refer to java.util. But a general policy of specific importing also solves the problem. There are many examples of this kind. The worst situation is to import too broadly and use a name that *doesn't* result in an error message, but creates code that is not what you intended. -- Paul Lutus http://www.arachnoid.com |
Re: single package import v/s the entire package
"Parvinder" <psarora@gmail.com> wrote in message
news:cgn7o4$o4f@odbk17.prod.google.com... > what is the advantage of importing a single class v/s all the classes > from the package > like java.util.date is better then java.util.*.If yes then what are the > advantages ? Look at Java 1.5 specification. Tiger even give you an option to import only static methods of classes. |
Re: single package import v/s the entire package
P.Hill coughed up:
> Parvinder wrote: >> what is the advantage of importing a single class v/s all the classes >> from the package >> like java.util.date is better then java.util.*.If yes then what are >> the advantages ? > > I'm surprised I couldn't find this in Roedy's Java Glossary. > > The different import statements have no effect at runtime, since all > code explicitly references the full path of a class. > > 1. Importing a class explicitly helps to point out exactly where a > class comes from. *BINGO*. The biggest win with import statements is to provide documentation to the poor sap reading your code just which class you mean. That it tell the compiler as much is also critical, but even in the cases where there is no name collision, you are *much* better off exhausting out a list. > > 2. Listing too many classes makes for a very cluttered list in > your code. > > 1 & 2 can be balanced using various schemes including: > a. import x.* from 'standard' packages only, where what is standard > may be project specific > b. import x.* when the number of classes from one package exceeds some > threshold, maybe 5 to 10. > > b is support by Eclipse. > > -Paul -- "His name was Robert Paulson. His name was Robert Paulson. His name was Robert Paulson..." |
| All times are GMT. The time now is 10:07 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.