Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Java Tip 50: On the Parameter Constants pattern

Combine nested interfaces and class constants for flexible design and easy access to constants

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Constants normally defined as static members of a class can alternatively be defined in an interface nested within the class. This design pattern allows us to achieve the otherwise conflicting goals, with respect to constants, of decoupling and proper containment. Any client class that makes substantial use of the constants can then implement the interface and use the constants without prefixing the class name. Classes that only need occasional use of the constants can continue to access them in the usual manner. These design patterns for constants are useful for categories of non-private constants such as parameters and prototypes. This design can be augmented with nested classes to provide type safety for parameter constants.

Classes often contain public static final fields (that is, "constants") used as parameters when invoking their methods and constructors. Java's own Calendar class provides a good example of constants for days of the week and months of the year. Take a look at some abbreviated source code for Calendar:

  public abstract class Calendar {
    public static final int SUNDAY = 1;
    public static final int MONDAY = 2;
    // and so on
    public static final int JANUARY = 0;
    public static final int FEBRUARY = 1;
    // and so on
    // constructors, methods, and fields follow
  }


Rather than embedding the constant definitions directly in the class, Java Tip 5:" Java constants," by John D. Mitchell, reminds us that it's possible to group together constants in an interface and implement the interface in a class in order to use unqualified references to the interface's constants. For example, client classes that work with Calendar may have many method invocations of Calendar with many references to the constants. For a given Calendar object, you can change the month with the following statement:

  aCalendar.set( Calendar.MONTH, Calendar.FEBRUARY );


However, if the client class implements an interface that defines the Calendar constants, you could directly refer to the constants without fully qualifying them as follows:

  aCalendar.set( MONTH, FEBRUARY );


This is how the changed design looks:

  public interface CalendarConstants {
    // fields in interface are implicitly public, static, and final
    int SUNDAY = 1;
    int MONDAY = 2;
    // and so on
    int JANUARY = 0;
    int FEBRUARY = 1;
    // and so on
  }
  public abstract class Calendar implements CalendarConstants {
    // constructors, methods, and fields follow
  }


This change to Calendar does not impact preexisting code that refers to the constants -- either code in Calendar itself or code in client classes that refer to the constants fully qualified, which is obviously helpful if you decide to transition a class to this approach. Client classes of Calendar are free to implement the interface and refer to the constants directly. You can also combine client classes that implement the interface with other client classes that fully qualify each constant. An example of such mixed usage can be found in the ScrollPaneConstants interface included in the Swing packages of the Java Foundation Classes (JFC). This coding shortcut comes in handy when you have a client class that works extensively with the constants from a class like Calendar -- though a few references to constants might not justify implementing the interface in a client class.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

waste of timeBy Anonymous on September 29, 2009, 10:49 amSorry, but did the author test the code provided? Obviously NOT. First of all it doesn't compile. Secondly, if it did compile, it wouldn't work anyway. SUNDAY, MONDAY...

Reply | Read entire comment

great article but some stuff does not work anymore in 2009?By Anonymous on August 30, 2009, 6:29 amI was trying your second example (Calendar implements Calendar.Parameters), but in Java 5.0 I get: "Cycle detected: the type Foo cannot extend/implement itself or...

Reply | Read entire comment

Great articleBy Anonymous on October 19, 2008, 7:39 amHi! As a Delphi programmer, I really missed its set definition. This is however a great substitute I was looking for. I hated having function parameters declared...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Inner Classes Specification
    http://www.javasoft.com/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc.html
  • For a review of how to group together constants in an interface and implement the interface in a class, see John D. Mitchell's "Java Tip 5Java constants" http://www.javaworld.com/javaworld/javatips/jw-javatip5.html
  • Read about patterns as they relate to type safe constants in Philip Bishop's "Java Tip 27Type safe constants in C++ and Java" http://www.javaworld.com/javaworld/javatips/jw-javatip27.html