A truly ugly utility method
The worst way to write the convertOzToMl() method (the way
that yields the maximum coupling) is to take an input from a
public static variable and put the output in
another public static variable. A
public static (but not final)
variable in Java is equivalent in functionality and danger to global
variables of C or C++. Here's an example:
// In source packet in file coupling/ex4/Liquid.java
// THIS APPROACH WORKS, BUT MAKES THE CODE HARD TO UNDERSTAND
// AND HARD TO CHANGE
class Liquid {
private static final double FL_OUNCES_PER_ML = 12.0/355.0;
private static final double ML_PER_FL_OUNCE = 355.0/12.0;
/**
* Converts fluid ounces to milliliters
*/
public static void convertOzToMl() {
double d = PurpleZebra.k * ML_PER_FL_OUNCE;
d += 0.5;
FlyingSaucer.q = (int) d;
}
}
// In source packet in file coupling/ex4/FlyingSaucer.java
class FlyingSaucer {
public static int q;
//...
}
// In source packet in file coupling/ex4/PurpleZebra.java
class PurpleZebra {
public static int k;
//...
}
To use the above version of convertOzToMl(), a programmer
would have to do the following:
// In source packet in file coupling/ex4/Example4.java
class Example4 {
public static void main(String[] args) {
PurpleZebra.k = 16;
Liquid.convertOzToMl();
int mlFor16Oz = FlyingSaucer.q;
System.out.println("Ml for 16 oz is: " + mlFor16Oz);
}
}