![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2008
Posts: 7
Rep Power: 0
![]() |
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?
__________________
Preposterous amounts of testosterone....Preposterone - powerthirst |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
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.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2008
Posts: 7
Rep Power: 0
![]() |
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?
__________________
Preposterous amounts of testosterone....Preposterone - powerthirst |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
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).
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2008
Posts: 7
Rep Power: 0
![]() |
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.
__________________
Preposterous amounts of testosterone....Preposterone - powerthirst |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2008
Posts: 6
Rep Power: 0
![]() |
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.
__________________
"Either write something worth reading or do something worth writing." -Benjamin Franklin Last edited by Shaythen; Mar 19th, 2008 at 1:47 PM. Reason: typos >< |
|
|
|
|
|
#7 |
|
12 years old
Join Date: Nov 2007
Posts: 94
Rep Power: 1
![]() |
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)
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
Re: Importing classes
yeah the * or per name is only a compile time issue not run time.
__________________
i dont know much about programming but i try to help |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Importing classes! | zanatos | Java | 3 | Jan 28th, 2008 10:36 PM |
| Could some please explain classes to me... | TCStyle | C++ | 10 | Feb 20th, 2006 3:51 PM |
| What is Lightweight C++? difference in inner classes between C++ and Java? | jonyzz | C++ | 4 | Sep 2nd, 2005 9:56 AM |
| List of classes | Dark_Shinobi | C++ | 8 | May 23rd, 2005 3:50 PM |
| C++ Classes - Quick question(s) | Delete | C++ | 8 | Apr 7th, 2005 7:38 PM |