www.webdeveloper.com
Results 1 to 12 of 12

Thread: Import whole package

  1. #1
    Join Date
    Dec 2002
    Location
    Taiwan
    Posts
    1,297

    Import whole package

    I had a question on the efficency of importing an entire package vs. just importing the classes you want to use. For example, let's say I have a Swing app that will make use of JFrame, JPanel, JButton, and JOptionPane. Does really matter which of the following methods I use?

    Way 1:
    Code:
    import javax.swing.*;
    Way 2:
    Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    I tend to use the second method in my code simply because I don't like importing classes I'm not using. Does it actually make any difference which I use as far as efficiency goes?
    The Web Standards Project Build accessible standard compliant websites, please!
    Browse Happy Don't forget to support the browsers with standard compliance

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Quote Originally Posted by Exuro
    ......
    Way 2:
    Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    ....
    This is the way to go, it is strongly recomended in the industry to not increase the compiled applications size if not needed.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Dec 2002
    Location
    Taiwan
    Posts
    1,297
    Quote Originally Posted by Khalid Ali
    This is the way to go, it is strongly recomended in the industry to not increase the compiled applications size if not needed.
    That's the thing though, Java isn't compiled to run stand-alone. It just reads the imported classes from the package stored on the user's computer, in this case one that would come with their version of the JRE. So what I'm worried about is when the program is running, is having the whole package imported actually going to be any different than importing each class you use individually?
    The Web Standards Project Build accessible standard compliant websites, please!
    Browse Happy Don't forget to support the browsers with standard compliance

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    I guess my answer was not clear enough, when you import a package all of the classes in it will be loaded in memory for any given class where they are imported, hence a huge overhead.If you import only what you need then there is no overhead whatsoever...hope this clears it
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    I actually thing Exuro has a point. I also thought that Java only referenced the location of the package, rather than actually dedicate any substancial amount of memory to it.

    Mayby I am misunderstanding.

    Regards.

  6. #6
    Join Date
    May 2004
    Location
    Perth, WA
    Posts
    171
    Heres a quote from Peter Haggar, a java expert from IBM (interestingly).

    The import statements do not affect the .class file size. They are used only to resolve references at compile time. Therefore, whether you use import a.b.*; or import a.b.c; doesn't matter.
    So, no memory or performance issues at all. When the classes are looked up at run time is when the performance will be an issue. (i.e. If you actually instantiated every class from an entire package obviously this will use a lot of memory, and processor)

    Hack the mod

  7. #7
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Quote Originally Posted by Oak
    ....
    Hack the mod
    Well not quite,Here is the deal, I have to stand corrected on mem issue. For some reason I have always under the impression that memory usage is the biggest issue behind explicit imports rather then package imports.
    Here is a real life problem that package import will cause in comparision with explicit import(explicit import , if its not obvious, is to import a single class).

    When you import packages such as
    Code:
    import java.util.*;
    import java.awt.*;
    
    
    and then if you reference 
    List list = new ArrayList(); //will cause a compile error ambigous reference
    where as explicit calls will not....
    So Exuro the path you have chosen is still the right one...
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  8. #8
    Join Date
    May 2004
    Location
    Perth, WA
    Posts
    171
    Yes, there are cases where it makes a difference as you have noted, however these are very rare and can be dealt with when encountered.

    For the sample originally quoted, I would encourage importing the whole package for brevity.

    Some people like to import each separate file so that they can see which package each is from, but this is a matter of personal preference.

    Mod Hack

  9. #9
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Quote Originally Posted by Oak
    Mod Hack

    Lets see now, will u say using log4j is personal choice is anymore?..hunmm lets think of something else..ummm how about JUnit...and not to mention ANT(which has become the most prevalent build tool). ofcourse they all can be personal choices but industry has chosen to use these tools/practices cus they have more benefits then not..hehehe
    And hence is the explicit import a better choice in 99.99% of the cases. If you have used any of the intelligent IDE's like eclipse, intelliJ etc they all will insert explict imports....wonder why...anyways...this it from me
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  10. #10
    Join Date
    Dec 2002
    Location
    Taiwan
    Posts
    1,297
    Thanks for the input everyone! I just wasn't sure if importing the whole package loaded references to every class in the package into memory, or if it just added that package into the "search path" to look for classes used in your code. Most of the time I'll still probably import individual classes, but I was getting tired of importing like 10 different classes from the javax.swing package, and then having to change the imports as I decided to use a JLayeredPane instead of a JPanel, ect. Thanks again!
    The Web Standards Project Build accessible standard compliant websites, please!
    Browse Happy Don't forget to support the browsers with standard compliance

  11. #11
    Join Date
    May 2004
    Location
    Perth, WA
    Posts
    171

    Smile

    Khalid Ali, I apologise. I have obviously offended you, however I feel there is still truth to what I have said.

    I also believe that the logging, testing and build-tool packages that a developer uses is a matter of personal taste. Thats the good thing about the open-source community. There are many options available.

    Using those intelligent ide's makes little difference to this topic because as you have stated they create explicit imports for you, thus it is no quicker to import the whole package, which was the reason I suggested it.

    In my programming experience, using struts, ibatis and other industry standard components I have had the lucky privilege of never (from memory) encountering an ambiguos import error.

    I feel that both of us have made valid points and that a person should make their own choice as a developer in a certain situation.

    All the best.

    Modulus Hackius

  12. #12
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    Quote Originally Posted by Oak
    ...I have obviously offended you, however I feel there is still truth to what I have said.
    Modulus Hackius
    You have not offended me at all, as a matter of fact eventhough I was doing write thing by importing explicit classes, for some reason it had been overlooked on my behalf to find out the truth about this issue,,,all along my career I learn a something new all the time, for which I thank u all here in this thread.....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles