Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 18th, 2008, 9:35 PM   #1
RainMan
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0 RainMan is on a distinguished road
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
RainMan is offline   Reply With Quote
Old Mar 18th, 2008, 9:57 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Old Mar 18th, 2008, 10:23 PM   #3
RainMan
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0 RainMan is on a distinguished road
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
RainMan is offline   Reply With Quote
Old Mar 18th, 2008, 11:28 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Old Mar 19th, 2008, 11:25 AM   #5
RainMan
Newbie
 
Join Date: Mar 2008
Posts: 7
Rep Power: 0 RainMan is on a distinguished road
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
RainMan is offline   Reply With Quote
Old Mar 19th, 2008, 1:46 PM   #6
Shaythen
Newbie
 
Shaythen's Avatar
 
Join Date: Mar 2008
Posts: 6
Rep Power: 0 Shaythen is on a distinguished road
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 ><
Shaythen is offline   Reply With Quote
Old Mar 19th, 2008, 8:15 PM   #7
null_ptr0
12 years old
 
Join Date: Nov 2007
Posts: 94
Rep Power: 1 null_ptr0 is on a distinguished road
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)
null_ptr0 is offline   Reply With Quote
Old Mar 20th, 2008, 4:15 AM   #8
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: Importing classes

Quote:
Originally Posted by null_ptr0 View Post
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)
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
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC