Mike Reddick

+ Follow
since Jun 06, 2007
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike Reddick

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 ]