1. Technology

Using Packages

By

Man using laptop computer
Sigrid Olsson/PhotoAlto Agency RF Collections/Getty Images

At some point every programmer needs to use a public class that is contained within a package. If the class is contained within a different package then the program must tell the compiler where to find it. There are three ways to reference a class in another package: use its fully qualified name, import the class, import the package.

Using the Fully Qualified Name

Using a fully qualified name is the same principle as giving the full location of a file by adding its directory path. For example, to use a JOptionPane all I need to know is its package name is javax.swing:

 javax.swing.JOptionPane.showMessageDialog( null, "I'm fully qualified!"); 

This is fine in a one off situation but if a class needs to use several JOptionPanes then writing the full name for each one is not very exciting for the programmer.

Import the Class

To avoid using the fully qualified name the packaged class can be specifically referenced. This is done using an import statement. Import statements are placed after the package statement but before the class declaration. To import just one class put its fully qualified name after the import keyword:

 package thisPackage;
 
 import javax.swing.JOptionPane;
 
 public class myClass{
 
   JOptionPane.showMessageDialog(null, "Just my class is imported!");
 } 

As you can see from the above code the JOptionPane in myClass can now be used by just using its class name.

Import the Package

An advantage of importing each class used in a program separately is that a programmer can easily see which ones are being used. However, if a lot of classes are needed from the same package it might make more sense to just reference the entire package. For example, if a class is designed to create a complex user interface then a lot of the classes in the javax.swing package might be needed. In this case its easier to import the entire package by adding an ".*" at the end of the import statement:

 package thisPackage;
 
 import javax.swing.*;
 
 public class myClass{
 
   JOptionPane.showMessageDialog(null, "My entire package is imported!");
 } 

The ".*" tells the compiler that the class might want to use any of the classes contained in the javax.swing package. It does not include the classes in any subpackages. For example, if the class needed to use the EtchedBorder class in the javax.swing.border package it would need to be imported separately:

 import javax.swing.*;
 import javax.swing.border.EtchedBorder; 

There is no run-time overhead incurred by importing the entire package – the compiled code will be the same size whether each individual class or the entire package is referenced. The choice is mainly about the readability of the code.

Using Classes With the Same Name

If there are two classes that share the same name but are imported from different packages, the compiler will get confused and the code will not compile:

 import apackage.Person;
 import anotherpackage.Person;
 

If both classes need to be used then use their fully qualified names to avoid compiler (and programmer) confusion:

 apackage.Person bob = new apackage.Person();
 anotherpackage.Person bill = new anotherpackage.Person(); 

Importing Static Fields and Methods

There is a shortcut for using static fields and static methods in a class. By adding the static modifier to the import statement the class name for the static field or static method is not needed. For example, all the color static fields can be imported by using:

 import static java.awt.Color.*; 

In the code the color constants can then be used without the need for the color class prefix:

 JButton pressMeButton = new JButton("Press Me");
 pressMeButton.setForeground(RED); 

Be careful with the use of static imports because they can cause readability issues for programmers new to the code. It can be hard to spot a static field without its class name attached.

  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Working With Objects
  6. Java Packages – Guide to Using Packages

©2014 About.com. All rights reserved.