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

neal ford @ javasummercamp 2008 Moscone Center - JavaOne dorade sjoerd_met_koffer golden_gate_warning_sign IMG_4606.JPG bonen_maler Joshua Bloch at JavaOne2008 IMG_4654.JPG red bar IMG_4597 olijfboom War Room - Diagrams Band Photo trap kikker (frog) javone 2008 closing keynote Stage being build in the nearby park bridge
View more photos >

Categories



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