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?
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?
This is the way to go, it is strongly recomended in the industry to not increase the compiled applications size if not needed.
Originally Posted by Exuro
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?
Originally Posted by Khalid Ali
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
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.
Heres a quote from Peter Haggar, a java expert from IBM (interestingly).
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)
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.
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.
Originally Posted by Oak
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
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...
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.
Originally Posted by Oak
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
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!
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.
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.....
Originally Posted by Oak
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)