Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   single package import v/s the entire package (http://www.velocityreviews.com/forums/t136431-single-package-import-v-s-the-entire-package.html)

Parvinder 08-27-2004 12:00 PM

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


Michael Borgwardt 08-27-2004 12:21 PM

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.

P.Hill 08-27-2004 12:46 PM

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



Pete Glasscock 08-27-2004 12:46 PM

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

Paul Lutus 08-27-2004 03:16 PM

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


Dejan Lazic 08-27-2004 05:16 PM

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.


Thomas G. Marshall 02-27-2005 02:02 PM

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.


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 47 48 49 50 51 52 53 54 55 56 57