Java Glossary : constants

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : C words : constants.

constants
Java is sloppy in its terminology. It muddles together the concepts of literals and constants. There are five different animals loosely called constants:
  1. Literals

    e.g.

    42, "abc"
    

    You may use byte, short, char and int literals as switch case labels. However longs and Strings are not supported.

  2. Compile Time Constants

    static finals whose value is known at compile time. e.g.

    static final int ARMS = 2;
    static final int LIMBS = ARMS * 2;
    

    These can be used as switch case labels, subject, of course, to the usual byte, short, char and int-only restriction.

  3. Class Constants

    static finals whose value is not known until class load time . e.g.

    static final int SERIALNUMBER = ++generator;
    

    These cannot used in switch case labels.

  4. Object Constants

    instance finals whose value is not known until object instantiation time . e.g.

    static final int DAY_CREATED = (int)(System.currentTimeMillis() / ( 24 * 60 * 60 * 1000L ));
    

    These too cannot used as switch case labels.

  5. Local Constants

    stack finals whose value is not known until the method executes. e.g.

    final int bottomLine = getTax();
    

    These too cannot used as switch case labels. I have not yet experimented to see if local constants of the form

    final int LEGS = 2;
    

    that the compiler knows at compile time, can be used in case labels.

Sun's coding conventions require constants to have names all in capital letters.

A key point that bears repeating is that constant references are constrained to point to the same object for their entire lifetimes. However, the object itself they point to is not necessarily constant. Its fields may or may not change. Whether they can change or not has nothing to do with whether the references that point to them are final.

See literals for how to specify strings, integers etc. Final in Java means a variable that will not change after it is initialised. The associated keywords with a named constant are static final. ENUMs in C++. They are constant values that have alphabetic names, e.g. Math.PI. For most practical purposes constants in Java are called "static final variables". However, only those static final variables whose value can be computed at compile time are true constants. Only true constants can be used as case labels. Note that constants always need to be qualified with the classname except in code inside the class. To further confuse the matter, the language specification refer to static final variables as "values" not constants.

Constants can be defined in both classes and interfaces. Interfaces without methods can be used simply as namespaces for related constants defined within them. For example, an interface MyConstants might define MAX_ROWS and MAX_COLS, accessed in other classes as MyConstants.MAX_ROWS and MyConstants.MAX_COLS. To access the constants in such an interface by unqualified name (e.g., MAX_ROWS) in methods of another class MyClass, you can make MyClass implement MyConstants. However, this trick comes with some costs, so should be used carefully. First, the constants become non-private class members of MyClass. Other classes can become dependent on MyClass implementing MyConstants -- an implementation detail. Second, other classes that access those public members depend on the values in the last-compiled version of MyClass, which can differ from the current values in the interface. Third, if MyClass implements multiple such interfaces, ambiguities can be created in MyClass during maintenance of those other interfaces.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 2497.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/constants.html J:\mindprod\jgloss\constants.html