Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to understand pros & cons of constant interfaces versus static imports  RSS feed

Mike Reddick
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heller and Roberts book on SCJP had the following in Chapter 1 - Language Fundamentals:

For example, an application that uses both English and metric weights might need the following:
public static float LBS_PER_KG = 2.2f;
public static float KGS_PER_LB = 1 / LBS_PER_KG;
Now the question is, where do these lines belong? The general answer to this question is that they belong in the most appropriate class or interface. Unfortunately, "most appropriate" doesn't always mean most convenient. Suppose you put our two lines in a class called Scales. Since the constants are in the namespace of Scales, they may appear there without prefix. For example, Scales might contain
massInPounds = massInKgs * LBS_PER_KG;
However, other classes must go to more trouble. Any class except Scales has to do the following:

They use an example to illustrate constant interfaces and static variables:

"massInPounds = massInKgs * Scales.LBS_PER_KG;

Many programmers, wishing to avoid the inconvenience of prefixing, have discovered the trick of creating an interface (known as a constant interface) to contain constants. This trick has two benefits. First, you don't have to decide which class to put the constants in; they go in the interface. Second, in any class that implements the constant interface, you don't have to prefix the constants. In our example, you might be tempted to place the constant definitions in an interface called Conversion. Then the Scales class, and all other classes that convert between pounds and kilos, can implement Conversion."

"Unfortunately, constant interfaces have several drawbacks. In the first place, to say that a class implements an interface really means that the class exposes the public methods listed in the interface. Interfaces are for defining types and should be used exclusively for that purpose. Constant interfaces only contain data, so they definitely don't define types."

"The second disadvantage is a bit more complicated. Suppose someone you work with writes some code that uses an instance of Scales. This person can legally reference that instance with a variable of type Conversion, even though doing so would be quite inappropriate. Later, if you wanted to eliminate the Conversion interface, you couldn't do so, because your misguided colleague would be relying on the existence of the interface."

Can anyone explain the second disadvantage of constant interfaces?

Don't static imports have the same disadvantage?

E.g. my co-worker could include:

import static Scales.LBS_PER_KG;

in his class so if I wanted to eliminate LBS_PER_KG from the Scales class and his class would no longer compile?

[ June 08, 2007: Message edited by: Mike Reddick ]
[ June 08, 2007: Message edited by: Mike Reddick ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!