Types of Constants | Importing Constants |
Compile Time vs Load Time Constants | Rules of Thumb |
Who Cares? | Links |
42, "abc"You may use byte, short, char and int literals as switch case labels. However, longs and Strings are not supported.
// constants known at compile time 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.
static final int SERIALNUMBER = ++generator;These cannot used in switch case labels. All the variables in an interface are implicitly static finals. They may be either compile time constants or load time constants.
final int bottomLine = getTax();These too cannot used as switch case labels.
final int LEGS = 2;As in the example above, it is sometimes possible for a local constant to be evaluated at compile time. In that case it is treated like a literal, much like a static compile-time constant. You can use a compile-time local int constant as a case label.
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. Constant values 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.
Compile Time vs Load Time Constants | |
---|---|
Expression | Compile Time or Load Time |
public static final int DAYS_IN_WEEK = 7; |
Compile Time |
public static final int SECONDS_PER_HOUR = 60 * 60; |
Compile Time |
public static final String COPYRIGHT = "Copyright (c) " + "2005"; |
Compile Time |
Compile Time | |
public static final int POSTAGE_IN_CENTS = 60; public static final int HANDLING_IN_CENTS = 500; public static final int BIGGER_IN_CENTS = ( POSTAGE_IN_CENTS > HANDLING_IN_CENTS ) ? POSTAGE_IN_CENTS : HANDLING_IN_CENTS; |
Compile Time |
Compile Time | |
Load Time | |
public static final Random wheel = new Random(); public static final int CARD = wheel.nextInt( 52 ); |
Load Time |
To find out whether other expressions will be evaluated at compile time, you can use a decompiler to see what sort of code was generated for the initialisation.
package.com.mindprod.backup.Backup; public class Backup { ... static final public int DAYS_BETWEEN_BACKUPS = 2; ... }
package com.mindprod.other.Other; import com.mindprod.backup.Backup; public class Other { ... int days = Backup.DAYS_BETWEEN_BACKUPS; ... }Note how you must specify the class name of the constant.
package com.mindprod.other.Other; import static com.mindprod.backup.Backup.DAYS_BETWEEN_BACKUPS; public class Other { ... int days = DAYS_BETWEEN_BACKUPS; ... }Note how you can leave off the class name of the constant. This feature is not available in prior to Java 1.5.
You cannot define static finals in an enum and use them in the enum constructors. You can put them in a separate private static nested class.
This page is posted |
http://mindprod.com/jgloss/constant.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\constant.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[18.224.30.6] |
| |
Feedback |
You are visitor number | |