Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 9
Some programs require class fields to refer to previously declared class fields. Java supports that activity by letting you specify the name of a previously declared class field in the expression portion of a subsequently declared class field's class field initializer, as Listing 4 demonstrates:
Listing 4. ClassInitializationDemo3.java
// ClassInitializationDemo3.java
class ClassInitializationDemo3
{
static int first = 3;
static int second = 1 + first;
public static void main (String [] args)
{
System.out.println ("first = " + first);
System.out.println ("second = " + second);
}
}
ClassInitializationDemo3 declares class field first and explicitly assigns 3 to that field. Then, ClassInitializationDemo3 declares class field second and refers to first in second's class initializer expression. When you run the program, you see the following output:
first = 3 second = 4
If you examine the byte code instructions for ClassInitializationDemo3's <clinit> method, you see something similar to Listing 5:
Listing 5. ClassInitializationDemo3's <clinit> method
0 iconst_3 1 putstatic ClassInitializationDemo3/first I // first = 3; 4 iconst_1 5 getstatic ClassInitializationDemo3/first I 8 iadd 9 putstatic ClassInitializationDemo3/second I // second = 1 + first; 12 return
Listing 5 shows byte code instructions that assign 3 to first and add constant 1 to first's contents. The result of that addition ends up in a temporary stack-based variable. The instructions subsequently assign
the contents of the temporary stack-based variable to second.
Although a subsequently declared class field can refer to a previously declared class field, the reverse is not true: You cannot declare a class field initializer that refers to a class field declared later in source code. In other words, Java does not permit forward references with class field initializers, as the following code fragment demonstrates:
static int second = 1 + first; static int first = 3;
When the compiler encounters either the code above or another forward reference code fragment during compilation, it generates
an error message because the developer's intention is unclear. Should the compiler regard first as containing 0, which results in second initializing to 1, or should the compiler regard first as containing 3, which results in second initializing to 4? To prevent that confusion, Java prohibits class field initializers from making forward references to other
class fields.
Although sufficient for class field initialization, class field initializers prove inadequate for more complex class initialization.
For example, suppose you need to read a file's contents into a buffer before the main() method executes. What do you do? Java meets that challenge by providing the class block initializer. A class block initializer consists of keyword static followed by an open brace character ({), initialization code, and a close brace character (}). Furthermore, a class block initializer appears within a class, but not within any of that class's methods, as Listing 6
demonstrates: