Static Initializer Blocks – Object Lifetime

10.7 Static Initializer Blocks

Java allows static initializer blocks to be defined in a class. Although such blocks can include arbitrary code, they are primarily used for initializing static fields. The code in a static initializer block is executed only once, when the class is loaded and initialized.

Local variables in static and instance initializer blocks can be declared with the reserved type name var—that is, local variable type inference using var is permitted for local variable declarations in initializer blocks. Code in the rest of this chapter shows many examples of such declarations.

The syntax of a static initializer block comprises the keyword static followed by a local block that can contain arbitrary code, as shown at (3) in the declaration of the following class:

Click here to view code image

class MatrixData {
  static final int ROWS = 12, COLUMNS = 10;          // (1)
  static long[][] matrix = new long[ROWS][COLUMNS];  // (2)
  // …
  static {                                           // (3) Static initializer
    for (int i = 0; i < matrix.length; i++)
      for (int j = 0; j < matrix[i].length; j++)
        matrix[i][j] = 2*i + j;
  }
  // …
}

When the class MatrixData is first loaded, the static final fields at (1) are initialized. Then the array of arrays matrix of specified size is created at (2), followed by the execution of the static block at (3).

Note that the static initializer block is not contained in any method. A class can have more than one static initializer block. Initializer blocks are not members of a class, and they cannot have a return statement because they cannot be called directly.

When a class is initialized, the initializer expressions in static field declarations and static initializer blocks are executed in the order in which they are specified in the class. In the class MatrixData, the initializer expressions at (1) and (2) are executed before the static initializer block at (3).

Similar restrictions apply to static initializer blocks as for static initializer expressions: The keywords this and super cannot occur in a static initializer block, as such a block defines a static context.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • 10.7 Static Initializer Blocks Java allows static initializer blocks to be defined in a class. Although such blocks can include arbitrary code, they are primarily used for initializing static fields. The code in a static initializer block is executed only once, when the class is loaded and initialized. Local variables in static and instance initializer…

  • 11.4 Wildcards In this section, we discuss how using wildcards can increase the expressive power of generic types. But first we examine one major difference between array types and parameterized types. The generic class Node<E> used in this subsection is defined in Example 11.2, p. 568. The Subtype Covariance Problem with Parameterized Types The following…

  • Exception Handling in Static Initializer Blocks Exception handling in static initializer blocks is no different from that in static initializer expressions: Uncaught checked exceptions cannot be thrown. Code in initializers cannot throw checked exceptions. A static initializer block cannot be called directly. Therefore, any checked exceptions must be caught and handled in the body of…

  • Extending Generic Types A non-final generic type can be extended. Example 11.5 shows that the generic interface IBiLink<E> extends the generic interface IMonoLink<E>, and that the generic class BiNode<E> extends the generic class MonoNode<E> and implements the generic interface IBiLink<E> (see Figure 11.1). Click here to view code image interface IBiLink<E> extends IMonoLink<E> {  //…

  • Generic Interfaces Generic types also include generic interfaces, which are declared analogous to generic classes. The specification of formal type parameters in a generic interface is the same as in a generic class. Example 11.3 declares a generic interface that defines the reference type IMonoLink<E> for objects that store a data value of type E.…

  • 11.1 Introducing Generics Generics allow classes and interfaces, as well as methods and constructors, to be parameterized with type information. An abstract data type (ADT) defines both the types of objects and the operations that can be performed on these objects. Generics allow us to specify the types used by the ADT so that the…

  • The Diamond Operator (<>) In the object creation expression of the new operator, the actual type parameter was explicitly specified after the class name—in contrast to the constructor declaration. Click here to view code image Node<String> lst = new Node<String>(“Hi”, null); // Explicit actual type parameter The actual type parameters can be omitted, but not…

  • Parameterized Local Variable Type Inference Consider the four local variable declarations shown below. The first three declarations are equivalent, as they create a Node<String> object whose data and next fields are null. In declaration (1), since the type String is explicitly specified in the object creation expression, the actual type parameter is deduced to be…

  • 11.2 Generic Types and Parameterized Types We first introduce the basic terminology and concepts relating to generics in Java. Note that the discussion here on generic and parameterized types also applies to enum types (§5.13, p. 287) and record classes (§5.14, p. 299). Generic Types A generic type is a reference type that defines a…

  • Subtype Contravariance: ? super Type The wildcard type ? super Type represents all supertypes of Type (including Type itself). The wildcard type ? super Type is called a lower bounded wildcard with Type representing its lower bound. Figure 11.4 Partial Type Hierarchy for Node<? super Integer> The wildcard type ? super Integer denotes all supertypes…