Results 1 to 8 of 8
  1. #1
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Why are some packages imported redundantly?

    I see this all the time in my java books. They'll use a wildcard operator, and then import one of the individual packages they just imported - again. Is this a stylistic practice, or do I not understand how wildcard operators work? I see this often in both Thinking in Java by Eckel and Java for Dummies.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Why are some packages imported redundantly?

    Can you give some examples so we can get a better idea which style of import that confuses you?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are some packages imported redundantly?

    They'll use a wildcard operator, and then import one of the individual packages they just imported
    Note that the asterisk style import imports all of the types in a package, it does not import multiple packages. Despite the hierarchical nature of package names, packages themselves are not especially related.

  4. #4
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Why are some packages imported redundantly?

    So it won't import the insides of a sub-package, only the types contained within that package?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why are some packages imported redundantly?

    I personally consider importing, say, java.util.* laziness when you want to import, say, java.util.Scanner. The import mechanism is a compiler thingy, so that it knows where to look for certain classes; it doesn't have any effect on the produced code size. Lucky enough Eclipse takes care of my import statements (other IDEs can do the same, I'm sure).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are some packages imported redundantly?

    Quote Originally Posted by Daimoth View Post
    So it won't import the insides of a sub-package, only the types contained within that package?
    Yes, exactly.

  7. #7
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Why are some packages imported redundantly?

    Quote Originally Posted by JosAH View Post
    I personally consider importing, say, java.util.* laziness when you want to import, say, java.util.Scanner. The import mechanism is a compiler thingy, so that it knows where to look for certain classes; it doesn't have any effect on the produced code size. Lucky enough Eclipse takes care of my import statements (other IDEs can do the same, I'm sure).

    kind regards,

    Jos
    While figuring this out I learned that wildcard imports won't affect performance or size in a major way, but they will clutter up your namespace.


    Anyways, thanks for the help. You guys rock, as usual.
    Last edited by Daimoth; 06-15-2012 at 10:10 PM.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why are some packages imported redundantly?

    Quote Originally Posted by Daimoth View Post
    While figuring this out I learned that wildcard imports won't affect performance or size in a major way, but they will clutter up your namespace.


    Anyways, thanks for the help. You guys rock, as usual.
    Namespaces are a compiler thing too; but you're right, it is unnecessary, so better import single classes instead of all the classes in a package.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Get Imported Classes with BCEL
    By Astrid in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2012, 05:06 PM
  2. Replies: 1
    Last Post: 04-03-2012, 09:29 AM
  3. problem with finding imported classes
    By frx08 in forum Java Applets
    Replies: 1
    Last Post: 01-19-2011, 11:51 AM
  4. Default imported packages
    By Himanshu23 in forum Java Software
    Replies: 2
    Last Post: 01-11-2011, 06:20 AM
  5. Replies: 3
    Last Post: 02-12-2008, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •