single package import v/s the entire package

P

Parvinder

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
 
M

Michael Borgwardt

Parvinder said:
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

P.Hill

Parvinder said:
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
 
P

Pete Glasscock

Michael Borgwardt said:
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
 
P

Paul Lutus

Parvinder said:
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.
 
D

Dejan Lazic

Parvinder said:
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.
 
T

Thomas G. Marshall

P.Hill coughed up:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top