Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: import

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

Java: import

Following the optional package declaration, you can have import statements, which allow you to specify classes that can be referenced without qualifying them with their package.

Packages are directories / folders that contain the Java classes, and are a way of grouping related classes together. For small programs it's common to omit a package specification (Java creates what it calls a default package in this case).

NetBeans 4.0 uses packages in several ways.

  • The project name is used for the package name.
  • A directory / folder is created with this project name. This directory name is the name of your package.
  • A package declaration is inserted into each source file.
  • When you build a main project, the double-clickable .jar file will use this project/package/directory name.

Syntax

The package-path is a dot-separated series of nested packages, eg, java.awt or java.awt.event. You can import (make visible) either a single class or all classes in package with the "*" wildcard character.
Suggestion: Use only the first wildcard case below. It is by far the most common usage.

   import package-path.*;     // Makes all classes in package visible.
   import package-path.class; // Makes only class visible.
   import static package-path.*;     // Makes all static variables in all classes in package visible.
   import static package-path.class; // Makes all static variables in class visible.

Example: import all classes in a package

The JOptionPane class is in the swing package, which is located in the javax package.

import javax.swing.*;  // Make all classes visible altho only one is used.

class ImportTest {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Hi");
        System.exit(0);
    }
}

Common imports

There are 166 packages containing 3279 classes and interfaces in Java 5. However, there are only a few packages that are used in most programming. GUI programs often use the first three imports.

import java.awt.*; Common GUI elements.
import java.awt.event.*; The most common GUI event listeners.
import javax.swing.*; More common GUI elements. Note "javax".
import java.util.*; Data structures (Collections), time, Scanner, etc classes.
import java.io.*; Input-output classes.
import java.text.*; Some formatting classes.
import java.util.regex.*; Regular expression classes.

Example: import only one class in a package

import javax.swing.JOptionPane;  // Make a single class visible.

class ImportTest {
    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Hi");
        System.exit(0);
    }
}

Example: Use explicit qualification instead of import

There is no need to use import when names are fully qualified. You will see some programs in this style, but it isn't as common because it makes source programs more congested and harder to read.

class ImportTest {
    public static void main(String[] args) {
        javax.swing.JOptionPane.showMessageDialog(null, "Hi");
        System.exit(0);
    }
}

import FAQ

  1. Q: Does importing all classes in a package make my object file (.class or .jar) larger?

    A: No, import only tells the compiler where to look for symbols.

  2. Q: Is it less efficient to import all classes than only the classes I need?

    A: No. The search for names is very efficient so there is no effective difference.

  3. Q: Doesn't it provide better documentation to import each class explicitly?

    A: This shows good intentions, but ...

    • It's hard to remember to remove classes when they are no longer used, so the import list is surprisingly often wrong. It can seriously slow down reading because unusual or unexpected class imports make me look for that class, only to discover that it must have been used in an earlier version.
    • Explicit class imports permit accidentally defining classes with names that conflict with the standard library names. This is very bad. Using "*" to import all classes prevents this dangerous naming accident.
    • It's annoying to always update this list.
  4. Q: I've imported java.awt.*, why do I also need java.awt.event.*?

    A: The wildcard "*" only makes the classes in this package visible, not any of the subpackages.

  5. Q: Why don't I need an import to use String, System, etc?

    A: All classes in the java.lang package are visible without an import.

  6. Q: Is the order of the imports important?

    A: No. Group them for readability.

Static imports in Java 5

Java 5 added an import static option that allows static variables (typically constants) to be referenced without qualifying them with a class name. For example, after

import static java.awt.Color;

It would then be possible to write

   Color background = RED;

instead of

   Color background = Color.RED;

Adding this "feature" wasn't the best idea because it leads to name pollution and confusion about which class constants come from. Even Sun (see References below) basically advises not to it!

References

  • Static Import, http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html.

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy