Java Reference
In-Depth Information
public class Words {
private Words() { }; // Uninstantiable
public static final String FIRST = ident( "the" );
public static final String SECOND = ident( null );
public static final String THIRD = ident( "set" );
private static String ident(String s) {
return s;
}
}
Despite their name, enum constants, introduced in release 5.0, are not constant variables. You can
add enum constants to an enum type, reorder them, and even remove unused enum constants
without the need to recompile clients.
In summary, constant variables are compiled into classes that reference those variables. A constant
variable is any primitive or string variable that is initialized with a constant expression.
Surprisingly, null is not a constant expression.
For language designers, perhaps it is not such a good idea to compile constant expressions into
clients in a language that is otherwise dynamically linked. It is surprising to many programmers and
can produce bugs that are difficult to diagnose: The source code in which constants were defined
may no longer exist when a bug is detected. On the other hand, compiling constant expressions into
clients does enable the use of if statements to emulate conditional compilation [JLS 14.21]. It is a
matter of judgment whether the end justifies the means.
< Day Day Up >
 
 
Search WWH ::




Custom Search