single package import v/s the entire package

Discussion in 'Java' started by Parvinder, Aug 27, 2004.

  1. Parvinder

    Parvinder Guest

    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
     
    Parvinder, Aug 27, 2004
    #1
    1. Advertising

  2. 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.
     
    Michael Borgwardt, Aug 27, 2004
    #2
    1. Advertising

  3. Parvinder

    P.Hill Guest

    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
     
    P.Hill, Aug 27, 2004
    #3
  4. > 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 <> 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
     
    Pete Glasscock, Aug 27, 2004
    #4
  5. Parvinder

    Paul Lutus Guest

    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
     
    Paul Lutus, Aug 27, 2004
    #5
  6. Parvinder

    Dejan Lazic Guest

    "Parvinder" <> wrote in message
    news:cgn7o4$...
    > 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.
     
    Dejan Lazic, Aug 27, 2004
    #6
  7. 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..."
     
    Thomas G. Marshall, Feb 27, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan
    Replies:
    1
    Views:
    311
    Karl Seguin
    Oct 20, 2004
  2. Crispy
    Replies:
    5
    Views:
    862
    e n | c k m a
    Aug 10, 2003
  3. Roedy Green
    Replies:
    1
    Views:
    341
  4. David Southwell
    Replies:
    3
    Views:
    152
    Phrogz
    Nov 4, 2009
  5. Sergey
    Replies:
    3
    Views:
    91
    Sergey
    Dec 13, 2013
Loading...

Share This Page