13.4.8
final
Fields and Constants
BINARY COMPATIBILITY
public static void main(String[] args) {
if (Flags.debug)
System.out.println("debug is true");
}
}
(One reason for requiring inlining of primitive constants is that Java
switch
 state 
ments require constants on each
case
, and no two such constant values may be
the same. Java checks for duplicate constant values in a
switch
 statement at com 
pile time; the
class
 file format does not do symbolic linkage of
case
 values.)
The best way to avoid problems with  inconstant constants  in widely distrib 
uted code is to declare as primitive constants only values which truly are unlikely
ever to change. Many primitive constants in interfaces are small integer values
replacing enumerated types, which Java does not support; these small values can
be chosen arbitrarily, and should not need to be changed. Other than for true math 
ematical constants, we recommend that Java code make very sparing use of class
variables that are declared
static
 and
final
. If the read only nature of
final
 is
required, a better choice is to declare a
private static
 variable and a suitable
accessor method to get its value. Thus we recommend:
private static int N;
public static int getN() { return N; }
rather than:
public static final int N = ...;
There is no problem with:
public static int N = ...;
if
N
 need not be read only. We also recommend, as a general rule, that only truly
constant values be declared in interfaces. We note, but do not recommend, that if a
field of primitive type of an interface may change, its value may be expressed idi 
omatically as in:
interface Flags {
boolean debug = new Boolean(true).booleanValue();
}
insuring that this value is not a constant. Similar idioms exist for the other primi 
tive types.
One other thing to note is that
static final
 fields that have constant values
(whether of primitive or
String
 type) must never appear to have the default initial
value for their type ( 4.5.4). This means that all such fields appear to be initialized
first during class initialization ( 8.3.2.1,  9.3.1,  12.4.2).
252






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved