SCJP Study Guide:
Declarations, Initialization and Scoping

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: Static Imports

Static Imports

Had you the coding experiences how to use the static fields and methods of classes in the previous Java version 1.4. For example, you must say:

double r = Math.cos(Math.PI * theta);

We invoked class Math's static fields and methods by preceding each with the class name Math and a dot (.).

In order to allow unqualified access to static fields in older Java version, you need to inherite class or interface. For example, consider the following interface that contains two constant values:

package com.xyzws.scjp;
public interface Constants {
public static final int PORTNUMBER = 80;
public static final String SERVICENAME = "PaymentService";

Now, the constants in the Constants interface can be used as follows:

public class SampleClass implements Constants {
System.out.println("Service Application Name : "

As you can see, a class must implement the interface in order to have access to the constants defined in the interface.

The static import feature enables you to import static members from a class or an interface and thus use them without a qualifying name. The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;

or en masse:

import static java.lang.Math.*;

Once the static members have been imported, they may be used without qualification:

double r = cos(PI * theta);

Let's also rewrite our SampleClass to use static imports:

import static com.xyzws.scjp.Constants.*;
import static java.lang.System.*;
public class SampleClass {
out.println("Service Application Name : "

A static import declaration has two forms?one that imports a particular static member (which is known as single static import) and one that imports all static members of a class (which is known as static import on demand). The following syntax imports a particular static member:

import static packageName.ClassName.staticMemberName;

where packageName is the package of the class (e.g., java.lang), ClassName is the name of the class (e.g., Math) and staticMemberName is the name of the static field or method (e.g., PI or abs).

While importing a particular static member is a nice addition, there are some situation that you want to import a lot of members. In such case, you can use *(wildcards) in static imports. The following syntax imports all static members of a class:

import static packageName.ClassName.*;

where packageName is the package of the class (e.g., java.lang) and ClassName is the name of the class (e.g., Math). The asterisk (*) indicates that all static members of the specified class should be available for use in the class(es) declared in the file. Note that static import declarations import only static class members. Regular import statements should be used to specify the classes used in a program.

Shadowing Static Imports

A declaration of an identifier named x within the scope of an identifier named x shadows the first identifier for the scope of the declaration. Access to a shadowed identifier requires qualification.

import static java.lang.System.err;

public class ShodowingStaticImports {
    public static void writeError(PrintStream err, String errMsg) {

Note that the variable err is defined as one of the parameter list for the writeError method. It will shadow the err variable imported from java.lang.System.

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |