• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import

 
Kris Reid
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kris Reid
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1033
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use checkstyle it will complain about import xx.*;
 
Josh Allen
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tommy Wan
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic