![]() |
Importing classes
Is there any advantages to simply importing a few portions of a class rather than the whole class?
If i'm not asking this correctly what i mean is this...as an example when you are importing parts of the javax.swing class is there a benefit to just importing specific portions over importing the whole thing? I could see memory usage by the program being the only issue but would that be significant enough to go through the trouble of importing ten parts of the class rather than the entire class? |
Re: Importing classes
First, a nitpick: javax.swing isn't a class, it's a package with a bunch of classes in it.
To answer your question: importing javax.swing.* brings all of the class names into scope. If you have any other classes with the same name, you'll get a conflict and the complier won't know which is which. It actually won't cause more memory usage, it'll just put extra names into scope. |
Re: Importing classes
Well the reason I ask is that the book I'm learning from imports classes on a name basis i.e. javax.swing.JFrame, javax.swing.JTextField, etc. What I'm wondering is why doesn't the book just have you import the entire package. It doesn't really go into detail about why it does it that way. It just seems like it would be easier to use javax.swing.* than list 8 class names.
It does the same thing for java.awt.* going through each class of the package that it uses, rather than importing it all. Is there a benefit to listing each individual class your referencing? or is it just something it is doing so we know what it is we're referencing within the package? |
Re: Importing classes
Doing each specific name prevents the namespace pollution I referred to. Some people also prefer it as a matter of style so you know exactly which classes are being used (or at least their use is intended).
|
Re: Importing classes
So do different packages use classes with the same name in them? Or is it more for when you define your own classes so that you don't accidentally use a class name thats in one of the packages you are using that you are unaware of.
Sorry if this is getting to be a tedious question but I like to make sure I completely understand something and unfortunately I'm as dense as I am intelligent...or more dense than intelligent sometimes. |
Re: Importing classes
Java books, especially books designed for classroom style learning are particular about these things because they want you to develop proper programming habbits. By specifically listing the class name and not just the package name your code is clearer to someone else who may have to work with it in the future. It seems in the real world of programming, there are more coders rewriting someone's old code than there are people writing code from scratch.
|
Re: Importing classes
java code conventions frown upon that, unless you are literally using a dozen or more classes from a package. as well as conventions, it also puts more stress on the compiler when it goes 'linking' (not really linking, just class finding)
|
Re: Importing classes
Quote:
|
| All times are GMT -5. The time now is 12:06 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC