Java 简明教程

Java - Instance Initializer Block

Java Instance Initializer Block

*instance initializer block*是在 class 中声明的代码块,用于初始化实例 data members。实例初始化器块对每个对象执行一次,可用于设置实例变量的初始值。

An instance initializer block is a block of code that is declared inside a class to initialize the instance data members. Instance Initializer block is executed once for each object and can be used to set initial values for instance variables.

instance initializer block 类似于 Java constructor,但其执行和用途不同。

The instance initializer block is similar to the Java constructor but its execution and uses are different.

Java Instance Initializer Block Example

这个例子演示了 Java 中的示例初始化程序块:

This example demonstrates instance initializer block in Java:

public class Tester {
   public int a;
   { a = 10; }
}

Characteristics of Instance Initializer Block

  1. Instance initializer block is called once an object is created.

  2. Instance initializer block is called before any constructor of an object is invoked.

  3. In case of child class, Instance initializer block will be called after super class constructor call.

  4. Instance initializer block can be used to execute multiple statements.

  5. Instance initializer block is generally used to instantiate multiple values fields like arrays.

Use of Instance Initializer Block

以下列出 Java 中实例初始化程序块的用途:

The following are the uses of instance initializer block in Java:

  1. To initialize the instance variable.

  2. To initialize the resources used in the code.

  3. To perform the dynamic initialization of the instance variables.

  4. To use the common initialization logic for multiple constructors.

Java Instance Initializer Block: More Examples

Example 1: Demonstrating What Invokes First, Instance Initializer Block or Constructor

在此示例中,我们展示了在对象构造函数之前执行实例初始化程序块。当使用 new 运算符创建对象时,将调用实例初始化程序块和构造函数。

In this example, we’ve shown that instance initializer block is getting executed before the object constructor. Both instance initializer block and constructor are invoked when object is created using new operator.

package com.tutorialspoint;
public class Tester {
   {
      System.out.println("Inside instance initializer block");
   }

   Tester(){
      System.out.println("Inside constructor");
   }

   public static void main(String[] arguments) {
      Tester test = new Tester();
   }
}
60
150

Example 2: Demonstrating Whether Constructor Overrides Instance Initializer Block

在此示例中,我们展示了实例初始化程序块中初始化的值被对象构造函数重写。当使用 new 运算符创建对象时,将调用实例初始化程序块和构造函数。

In this example, we’ve shown that a value initialized in instance initializer block is getting overriden by the object constructor. Both instance initializer block and constructor are invoked when object is created using new operator.

package com.tutorialspoint;
public class Tester {
   int a;
   {
      System.out.println("Inside instance initializer block");
      a = 10;
   }

   Tester(){
      System.out.println("Inside constructor");
      a = 20;
   }

   public static void main(String[] arguments) {
      Tester test = new Tester();
      System.out.println("Value of a: " + a);
   }
}
Inside instance initializer block
Inside constructor
Value of a: 20

Example 3: Instance Initializer Block and Super Constructor

在此示例中,我们展示了在子实例初始化程序块之前调用超类构造函数。我们创建了一个 SuperTester 类,而 Tester 类扩展了此类。在 main() 方法中,我们将打印实例变量的值。在输出中,可以验证调用的块的顺序。首先调用超类构造函数。然后调用子实例初始化器,该初始化器初始化实例变量,然后调用子类的构造函数。

In this example, we’ve shown that a super constructor is invoked before child instance initializer block. We’ve created a SuperTester class and Tester class extends this class. In main() method, we’re printing the value of instance variable. In output, you can verify the order of blocks invoked. First super constructor is invoked. Then child instance initializer is invoked which initializes an instance variable and then constructor of child class is invoked.

package com.tutorialspoint;
class SuperTester{
   SuperTester(){
      System.out.println("Inside super constructor");
   }
}

public class Tester extends SuperTester {
   int a;
   {
      System.out.println("Inside instance initializer block");
      a = 10;
   }

   Tester(){
      System.out.println("Inside constructor");
   }

   public static void main(String[] arguments) {
      Tester test = new Tester();
      System.out.println("Value of a: " + test.a);
   }
}
Inside super constructor
Inside instance initializer block
Inside constructor
Value of a: 10