Nested interfaces for grouping constants

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!


72 Responses to “Nested interfaces for grouping constants”

  1. 1 Frans Maas

    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!

  2. 2 peter

    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’

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures

IMG_4618.JPG bridge _MG_8016 IMG_6147 Golden Gate flower DHH via IChat IMG_4552.JPG Band Photo Sjoerd met speer nearby hotel Picture 2 oldtimer Sjoerd op de Sallandse Heuvelrug (en de rug van papa) trap (II) Moscone Center - JavaOne nice place to park a bike IMG_4597 robin_met_zijn_hyundai_horloge_foon
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.