Saturday, 7 July 2012

Item 19: Use interfaces only to define types

When a class implements an interface, the interface serves as a type that can be used to refer to instances of the class. That a class implements an interface should therefore say something about what a client can do with instances of the class. It is inappropriate to define an interface for any other purpose.

One kind of interface that fails this test is the so-called constant interface. Such an interface contains no methods; it consists solely of static final fields, each exporting a constant. Here is an example:

// Constant interface antipattern - do not use!
public interface PhysicalConstants {
// Avogadro's number (1/mol)
static final double AVOGADROS_NUMBER = 6.02214199e23;
// Boltzmann constant (J/K)
static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
// Mass of the electron (kg)
static final double ELECTRON_MASS = 9.10938188e-31;

The constant interface pattern is a poor use of interfaces. There are several constant interfaces in Java platform libraries, such as

If you want to export constants, there are several reasonable choices. Integer and Double, export MIN_VALUE and MAX_VALUE constants. If the constants are best viewed as members of an enumerated type, you should export them with an enum type (Item 30). Otherwise, you should export the constants with a noninstantiable utility class (Item 4). Here is a utility class version of the PhysicalConstants example above:

// Constant utility class
public class PhysicalConstants {
private PhysicalConstants() { } // Prevents instantiation
public static final double AVOGADROS_NUMBER = 6.02214199e23;
public static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
public static final double ELECTRON_MASS = 9.10938188e-31;

Normally a utility class requires clients to qualify constant names with a class name, you can avoid the need for qualifying the constants with the class name by making use of the static import facility, introduced in release 1.5:

// Use of static import to avoid qualifying constants
import static*;
public class Test {
double atoms(double mols) {
return AVOGADROS_NUMBER * mols;
// Many more uses of PhysicalConstants justify static import

In summary, interfaces should be used only to define types. They should not be used to export constants.

Reference: Effective Java 2nd Edition by Joshua Bloch