THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

2.5.3. Static Initialization


The static fields of a class can have initializers as we have already seen. But in addition we can perform more
complex static initialization in a static initialization block. A static initialization block is much like a
non-static initialization block except it is declared static, can only refer to static members of the class, and
cannot throw any checked exceptions. For example, creating a static array and initializing its elements
sometimes must be done with executable statements. Here is example code to initialize a small array of prime
numbers:


class Primes {
static int[] knownPrimes = new int[4];


static {
knownPrimes[0] = 2;
for (int i = 1; i < knownPrimes.length; i++)
knownPrimes[i] = nextPrime();
}
// declaration of nextPrime ...
}


The order of initialization within a class is first-to-lasteach field initializer or initialization block is executed
before the next one, from the beginning of the source to the end. The static initializers are executed after the
class is loaded, before it is actually used (see "Preparing a Class for Use" on page 441). With this guarantee,
our static block in the example is assured that the knownPrimes array is already created before the
initialization code block executes. Similarly, anyone accessing a static field is guaranteed that the field has
been initialized.


What if a static initializer in class X invokes a method in Y, but Y's static initializers invoke a method in X to
set up its static values? This cyclic static initialization cannot be reliably detected during compilation because
the code for Y may not be written when X is compiled. If cycles happen, X's static initializers will have been
executed only to the point where Y's method was invoked. When Y, in turn, invokes the X method, that
method runs with the rest of the static initializers yet to be executed. Any static fields in X that haven't had
their initializers executed will still have their default values (false, '/u0000', zero, or null depending
on their type).


2.6. Methods


A class's methods typically contain the code that understands and manipulates an object's state. Some classes
have public or protected fields for programmers to manipulate directly, but in most cases this isn't a
very good idea (see "Designing a Class to Be Extended" on page 108). Many objects have tasks that cannot be
represented as a simple value to be read or modified but that require computation.


We have already seen a number of examples of methods in Chapter 1all of our demonstration programs had a
main method that was executed by the Java virtual machine. Here is another main method that creates a
Body object and prints out the values of its fields.


class BodyPrint {
public static void main(String[] args) {
Body sun = new Body("Sol", null);
Body earth = new Body("Earth", sun);
System.out.println("Body " + earth.name +
" orbits " + earth.orbits.name +
" and has ID " + earth.idNum);
}
}

Free download pdf