Home : Course Map : Chapter 5 : Java :
Static Import - J2SE5.0
JavaTech
Course Map
Chapter 5
File/Package/Import
  Demo 1
  Demo 2
Access/Visibility
final & Constants
Static Import
Jar Files
  Demo 3
Applet Directories
3rd Party Packages
CLASSPATH
javadoc
CodingConventions
Exercises

    Supplements
Scope
Debug Techniques
Java Runtime
Class Class
JVM Instructions 2
JVM Processing
pack200

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Many classes, including many in the Java core libraries, contain static constants that are used within the class and are also useful outside the class. For example, the java.lang.Math class contains the constants PI and E for pi and e, respectively.

As another example, we see in Chapter 7 that the BorderLayout class contains constants such as NORTH, CENTER, etc. that are useful for laying out graphics components.

Prior to Java version 5.0, the only way to access those constants was by fully spelling out the names Math.PI, Math.E, BorderLayout.NORTH, etc. in your code. With static imports, you can use just PI, E, and NORTH without all the extra typing. To use static imports, add the static keyword to the import statement as follows:

  import static java.lang.Math.PI;

Then in the code body you can use

  double area = PI * radius * radius;

instead of

  double area = Math.PI * radius * radius;

Wildcards also work in the import static statement:

  import static java.lang.Math.*;

For occasional use, simply typing the Math.PI is probably easier, but if constants from another class are used extensively, then the static import feature will reduce the code and also make it easier to read.

In addition to the constants, all the many static methods in the Math class are also available when you use the wildcard static import line above. For example,

  double logarithm = log (number);

instead of

  double logarithm = Math.log (number);

For extensive use of the mathematical functions, the static import feature is a great addition to the Java language.

Interfaces with Constants Only

Before the static import became available, a trick for avoiding the need to write the class name for static constants was to create an interface whose only purpose is to hold constants.

For example, say that you need the constants ALPHA and OMEGA for several different programs. The first option is to create a utility class that holds the constants. e.g.:

  public class MyConstants
  {
     final static double ALPHA = 1.1;
     final static double OMEGA = 901.0
  
}

Then in your other classes, you would refer to these constants with

  MyConstants.ALPHA
  MyConstants.OMEGA

However, placing the MyConstants class name wherever these constants appear can be tedious and also makes the code less readable, especially in a formula like

   x = (3.1 * MyConstants.ALPHA)/(1.0 + MyConstants.OMEGA);

An alternative is to put the constants into an interface. This will not interfere with the class design since there is no limit to the number of interfaces that a class can implement.

  public interface MyConstantsImpl
  {
    static final double ALPHA = 1.1;
    static final double OMEGA = 901.0
  }

Any class that needs these constants can implement MyConstantsImpl. The methods in the program will then refer to the constants simply with ALPHA and OMEGA.

Then a method in the class that implements MyConstantsImpl could use the constants in an equation without the class reference:

   x = (3.1*ALPHA)/(1.0 + OMEGA);

whcih is far more readable.

This technique, however, violates the object-oriented design of the language. That is, you are not really implementing anything. You would not think of a class that implements MyConstantsImpl as a MyConstants object in the sense of taking on an identifiable behavior of an interface.

So constants-only interfaces are ill advised and considered bad programming style . Use a class instead and then statically import the class.

References & Web Resources

Last update: Oct. 19, 2005

            Tech
DecimalFormat
  Demo 1
  Demo 2

System.out.printf
  Demo 3
CJ Format Class
  Demo 4   Demo 5
Other Format Tools

Immutable Complex
Exercises

           Physics
Interpolation
Integration
  Demo 1
Simpson Rule
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.