#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    410
    Rep Power
    24

    Constant Questions


    1. I would like to group all constant values into one single point so that they can be easily maintained. Should I put them into Class or Interface? which one? any trade off?

    2. Although they are constant values, they may be configurable dependent to different regions. How can I handle them? Can I do the following?

    Code:
    public Interface Constants {
       static public final String value1 = Properties.load("value1");
       static public final String value2 = Properties.load("value2");
    }
    Is there any performance issue?
    ------------------------------------------
    Perl Kids Kiss Perl
    Stanley
    ------------------------------------------
  2. #2
  3. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,557
    Rep Power
    1626

    Suggestions


    1. It is up to you. But having constants defined in an interface prevents other classes from instantiating objects as one cannot instantiate new objects directly from interfaces using the keyword new .

    2. I think it should be fine. But I would prefer to put these properties into class-level constants, since it only loads the properties when the class is loaded.
    When the programming world turns decent, the real world will turn upside down.
  4. #3
  5. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    But having constants defined in an interface prevents other classes from instantiating objects as one cannot instantiate new objects directly from interfaces using the keyword new .
    One can however implement that interface, which is "a poor use of interfaces" (see Effective Java 2ED, Item #19). Better to define the constants in the class they relate to, or a utility class. It's easy to prevent instantiation of the utility class by simply providing a private no-arg constructor. Example:
    Java Code:
    package com.yawmark.util;
     
    public class ApplicationConstants {
        private ApplicationConstants() {
            // prevent instantiation
        }
     
        public static final int THE_ANSWER_TO_LIFE_UNIVERSE_AND_EVERYTHING = 42;
    }


    ~

    Comments on this post

    • tfecw agrees
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    410
    Rep Power
    24
    Does Class use more resource, say CPU or memory or IO, than Interface?

    Originally Posted by Yawmark
    One can however implement that interface, which is "a poor use of interfaces" (see Effective Java 2ED, Item #19). Better to define the constants in the class they relate to, or a utility class. It's easy to prevent instantiation of the utility class by simply providing a private no-arg constructor. Example:
    Java Code:
    package com.yawmark.util;
     
    public class ApplicationConstants {
        private ApplicationConstants() {
            // prevent instantiation
        }
     
        public static final int THE_ANSWER_TO_LIFE_UNIVERSE_AND_EVERYTHING = 42;
    }


    ~
    ------------------------------------------
    Perl Kids Kiss Perl
    Stanley
    ------------------------------------------
  8. #5
  9. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Does Class use more resource, say CPU or memory or IO, than Interface?
    No.

    On a related topic, consider the following, particularly the "Wrong Intuitions About Performance Problems" and "Write Dumb Code" sections:

    http://java.sun.com/developer/techni.../goetz_qa.html

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo