Where to define application-wide constants

Discussion in 'Java' started by Ahmed Moustafa, Aug 10, 2003.

  1. Where should application-wide contants be defined e.g. in a class or an

    Thanks in advance!
    Ahmed Moustafa, Aug 10, 2003
    1. Advertisements

  2. Ahmed Moustafa

    Joe Guest

    They should be public static final members of an abstract class.
    Joe, Aug 10, 2003
    1. Advertisements

  3. Why abstract class?
    Ahmed Moustafa, Aug 10, 2003
  4. Ahmed Moustafa

    Roedy Green Guest

    Java 1.5 has a new answer to that old question.
    Roedy Green, Aug 10, 2003
  5. enum?
    Ahmed Moustafa, Aug 10, 2003
  6. I tend to put them in an interface. But it really depends on the
    values and their relationship to the rest of the code. It sometimes
    makes sense to define public static final items in the classes which
    use them, but stuff that is truly global to an app kinda cries out
    for an interface of their own. YMMV

    Joseph Millar, Aug 10, 2003
  7. Ahmed Moustafa

    Roedy Green Guest

    no, a way of exposing constants so they don't need the class name
    Roedy Green, Aug 14, 2003
  8. Ahmed Moustafa

    Chris Smith Guest

    You mean "import static"? I agree that, all things considered, it's far
    better to use "import static" than implement an interface full of
    constants. Nevertheless, I wonder about the desire to hide context in
    the first place. The net effect is to make it more difficult to assume
    that a method or variable is directly related to the current class.

    That said, I'd much rather have "import static" than a solution that
    forces the use of unqualified constant or method names.

    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Aug 14, 2003
  9. How about the Singleton pattern? Does it make any sense to implement a
    singleton to hold global constants/variables?
    Ahmed Moustafa, Aug 17, 2003
  10. My constants include matrices that I load from a text files, I hold
    these matrices in a class that extends HashMap and the keys are the
    names of the files.

    The class is going to be Singleton because I need only one instance per JVM.

    Should I load all the text files in the constructor, which happens only
    once? Or, should I load each file upon demand and if it has been already
    loaded, I don't load it again?
    Ahmed Moustafa, Aug 17, 2003
  11. Ahmed Moustafa

    Roedy Green Guest

    You can still implement this as purely static variables. They will be
    loaded the first time you use the class. There is no need to go to
    the hassle of creating a singleton object.

    The hassle may be worthwhile when you plan to subclass the singleton
    object, or if you suspect the object won't always be a singleton.
    Roedy Green, Aug 17, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.