Final Fields in Classes
A final field must be explicitly initialized only once with an initializer expression, either in its declaration or in an initializer block. A final instance field can also be initialized in a constructor. After the class completes initialization, the final static fields of the class are all guaranteed to be initialized. After a constructor completes execution, the final instance fields of the current object are all guaranteed to be initialized. The compiler ensures that the class provides the appropriate code to initialize the final fields.
In Example 5.9, the class Light defines two final static fields at (2) and (3). The field KWH_PRICE is a constant variable that is initialized by the constant expression 3.25 in the declaration at (2). The field MANUFACTURER is a blank final static field that is initialized in the static initializer block at (4). All static initializer blocks are executed at class initialization. Note that a blank final static field must be initialized in a static initializer block; otherwise, the compiler will issue an error. An attempt to change the value of the final static field KWH_PRICE at (11) in a method results in a compile-time error.
In Example 5.9, the class Light also defines two final instance fields at (6) and (7). The instance field color is a blank final instance field that is initialized in the instance initializer block at (8), and the instance field energyRating is a blank final instance field that is initialized in the constructor at (9). If either the assignment at (8) in the instance initializer block or the assignment at (9) in the constructor is removed, it will result in a compile-time error.
Note that a blank final instance field must be initialized either in an instance initializer block or in a constructor; otherwise, the code will not compile. Each time an object is created using the new operator with a constructor call, all instance initializer blocks are executed, but not necessarily all constructors, as this depends on constructor chaining. If the class has several constructors, code must make sure that a blank final instance field is initialized no matter which constructor is called to initialize the object.
Since final static fields are initialized at class initialization time and final instance fields are initialized at object creation time, the compiler will issue an error if an attempt is made to assign a value to a final field inside any method.
Analogous to a non-final field, a final field with the same name can be hidden in a subclass—in contrast to final methods that can neither be overridden nor hidden. In Example 5.9, the blank final instance field color of type String at (6) in class Light is hidden by the static field color of type StringBuilder at (13) in subclass TubeLight.
Fields that are final and static are commonly used to define manifest constants (also called named constants). For example, the minimum and maximum values of the numerical primitive types are defined by final static fields in the respective wrapper classes. The final static field Integer.MAX_VALUE defines the maximum int value. The field java.lang.System.out is a final static field. Fields defined in an interface are implicitly final static fields (p. 254), as are the enum constants of an enum type (p. 287).