4

Since I'm still new to Java, I'm running into some need for clarification how imports should be done properly.

First:

import java.awt.*;
import java.swing.*;

I'm assuming the * means anything under the "awt" and "swing" directory. But I've seen people doing this before:

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

Am I misunderstanding something? Or am I simply finding redundancy?

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
coffeemonitor
  • 12,780
  • 34
  • 99
  • 149
  • 5
    Using Eclipse, you can and should take advantage of automatic package organization (Ctrl-Shift-O or Command-Shift-O, depending on your platform). Having said that, read [here](http://stackoverflow.com/questions/8550214/what-is-the-impact-of-redundant-import-statements-in-java) for information directly regarding to your question. – Perception Dec 18 '12 at 01:56
  • Great advice - Perception. After doing the keystrokes, I can see the necessary imports up top. Nice feature – coffeemonitor Dec 18 '12 at 02:41

4 Answers4

4
import javax.awt.*

Will include all of the classes in the package javax.awt however it will not inclue any other packages nested inside of javax.awt such as javax.awt.event. You need to use that as a separate import. All packages of different names needed to be included separately.

Using import javax.swing.* you will not need the following imports:

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

because they are direct children of the swing package.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
2

In an import statement, the * indicates that you want everything from that package. In general, this isn't a good idea - you don't need everything from the package you're including.

In practice, I've seen imports optimized - merely import what you need. Most IDEs, such as Eclipse, Netbeans and IntelliJ have options to do just that for you.

Makoto
  • 104,088
  • 27
  • 192
  • 230
2

It's usually a good idea to use explicit imports and avoid using wildcards, so that you always know exactly what you are importing. However, the JVM will only import the required classes when you use wildcards (but not from sub packages), so there is no loss in efficiency (aside from a negligible compilation overhead).

It's not necessarily redundant to do this, but in my opinion a bit awkward:

import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.*;

There might be another UnsupportedLookAndFeelException declared in another package so you want to be sure to include the one from javax.swing package, so it's made explicit in this case. Your best bet as mentioned is to allow the IDE to help you explicitly choose the import you want.

Bizmarck
  • 2,663
  • 2
  • 33
  • 48
2

When you add * at the end of the import (wild card import)you are importing all classes included in certain package i.e com.test.* . On the other hand when you add for example a class you will specifically import that particular class i.e com.test.TestClass

However, when using the first approach you need to be aware of possible class collisions as explained in this stackoverflow thread: Eclipse/Java - is it harmful to import java.(namespace).*?

Due to that I prefer the second approach since I can explicitly tell which class I intend to use, and in that way it makes the code easier to read

Community
  • 1
  • 1
Bartzilla
  • 2,768
  • 4
  • 28
  • 37