Nested interfaces for grouping constants
Published by peter February 21st, 2006 in java.For the project we’re working on we can not use Java 5; customer demands. This means we can’t use the enum type to group constants. A colleague of mine pointed out that nested interfaces can provide very similar results:
public interface Constants{
public interface A{
String CONSTANT_ONE = “asdfasdf’;
String CONSTANT_TWO = “asdfasdf’;
}
public interface B{String CONSTANT_ONE = “asdfasdf’;
String CONSTANT_TWO = “asdfasdf’;
}
}
Now it’s possible to do stuff like:
Constants.A.CONSTANT_ONE
Constants.B.CONSTANT_ONE
nice touch!



















But this is not type safe and you can not enumerate like:
for (A c : A.values()) System.out.printf(”Constant %s = %s”,c, c.x));
But anyway, the Customer is King of course!
Someone has been doing his homework
Of course the for-each construct isn’t availlable in java 1.4.2 either, as is the printf on the standard output stream.
And customers seem to be reluctant to change working versions… although Java 5 is much faster, and the enhanced semantics make code much easier to read resulting in higher quality applications…….
Hard to understand this ‘customer’