File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "import" Watch "import" New topic
Author

import

Kris Reid
Ranch Hand

Joined: Jan 05, 2005
Posts: 247
What is the best practice to use for importing? i.e. What should I use for the project.

I downloaded a swing demo class from Sun and noticed they import tyhe following:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;


Why do the import each individual class from swing and then just the whole awt & event? Why don't they just have:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
They probably did it that way to make the code more "self-documenting". A reader can readily see which Swing classes were imported, but we can assume the AWT stuff is only for event handling, so we don't need to see the list of AWT classes.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Kris Reid
Ranch Hand

Joined: Jan 05, 2005
Posts: 247
So when building a gui is it ok to have

import javax.swing.*;

or should I use

import javax.swing.JButton;


sorry to sound picky I just want everything to be perfect
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I think which way you import is largely a matter of personal preference. It's been a while since I read the Java coding conventions on Sun's website, but I don't remember them having an entry on import conventions. However, you may want to check because the assignment will in part be graded on conformance to the Java coding style specifications. Personally, I like importing individual classes because then I don't have to comment why I'm importing a package.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
If you use checkstyle it will complain about import xx.*;
Josh Allen
Ranch Hand

Joined: Jan 15, 2005
Posts: 37
"Java 2 Perforamance and Idiom Guide" has a section (15.3) on this, and they have make a good argument for always using the import-on-demand (.*) type (unless of course you can't because of a naming conflict.)

The conclude:
Liberate yourselves from the tyranny of of the single-type-import! Declare your imports with type-imporst-on-demand and get to coding!


In my own experience after having used a utility for a while that would change all your import on demand (.*) to single type imports I ended up just prefering the import on demand.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
If I am only importing one class, I will just import the class, not the whole package. But if I have two or more from the same package, I will import-on-demand (Never knew that's what it was called until now, thanks Josh!).

Jeff, you mentioned that you don't like commenting why you import a package. Is that really a standard practice? I have never encountered it before. I can see where it would be useful, but at the same time, I can always simply look at the APIs if I am unsure of a package contents. Just wnated to hear some opinions on it.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Tommy Wan
Greenhorn

Joined: Jan 05, 2005
Posts: 14
It does not really matter. Import only create a pointer to the class or the package, you don't really load anything at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import