10.4. Interfaces

 
[Page 327 ( continued )]

9.15. (Optional) Initialization Blocks

Initialization blocks can be used to initialize objects along with the constructors. An initialization block is a block of statements enclosed inside a pair of braces. An initialization block appears within the class declaration, but not inside methods or constructors. It is executed as if it were placed at the beginning of every constructor in the class.

Initialization blocks can simplify the classes if you have multiple constructors sharing a common code and none of them can invoke other constructors. The common code can be placed in an initialization block, as shown in the example in Figure 9.10(a). In this example, none of the constructors can invoke any of the others using the syntax this(...) . When an instance is created using a constructor of the Book class, the initialization block is executed to increase the object count by 1. The program is equivalent to Figure 9.10(b).

Figure 9.10. An initialization block can simplify coding for constructors.
(This item is displayed on page 328 in the print version)

Note

A class may have multiple initialization blocks. In such cases, the blocks are executed in the order they appear in the class.


The initialization block in Figure 9.10(a) is referred to as an instance initialization block because it is executed whenever an instance of the class is created. A static initialization block is much like an instance initialization block except that it is declared static , can only refer to static members of the class, and is executed when the class is loaded. The JVM loads the class dynamically when it is needed. A superclass is loaded before its subclasses. The order of the execution can be summarized as follows :

  1. When a class is used for the first time, it needs to be loaded. Loading involves two phases:


    [Page 328]
    1.1. Load superclasses. Before loading any class, its superclass must be loaded if it is not already loaded. This is a recursive process until a superclass along the inheritance chain is already loaded.
    1.2. After a class is loaded to the memory, its static data fields and static initialization block are executed in the order they appear in the class.

  2. Invoking a constructor of the class involves three phases:

    2.1. Invoke a constructor of the superclass. This is a recursive process until the superclass is java.lang.Object .
    2.2. Initialize instance data fields and execute initialization blocks in the order they appear in the class.
    2.3. Execute the body of the constructor.

Listing 9.11 demonstrates the execution order of initialization blocks.

Listing 9.11. InitializationDemo.java
(This item is displayed on pages 328 - 329 in the print version)
 1   public class   InitializationDemo { 2   public static void   main(String[] args) { 3   new   InitializationDemo(); 4 } 5 6   public   InitializationDemo() { 7   new   M(); 8 } 9 10 { 11 System.out.println(   "(2) InitializationDemo's instance block"   ); 12 } 13 14   static   { 15 System.out.println(   "(1) InitializationDemo's static block"   ); 16 } 17 } 18 19   class   M   extends   N { 20 M() { 

[Page 329]
 21 System.out.println(   "(8) M's constructor body"   ); 22 } 23 24 { 25 System.out.println(   "(7) M's instance initialization block"   ); 26 } 27 28   static   { 29 System.out.println(   "(4) M's static initialization block"   ); 30 } 31 } 32 33   class   N { 34 N() { 35 System.out.println(   "(6) N's constructor body"   ); 36 } 37 38 { 39 System.out.println(   "(5) N's instance initialization block"   ); 40 } 41 42   static   { 43 System.out.println(   "(3) N's static initialization block"   ); 44 } 45 } 

The output of this program is:

 (1) InitializationDemo  '  s static block (2) InitializationDemo  '  s instance block (3) N  '  s static initialization block (4) M  '  s static initialization block (5) N  '  s instance initialization block (6) N  '  s constructor body (7) M  '  s instance initialization block (8) M  '  s constructor body 

The program is executed in the following order:

  1. The superclass of InitializationDemo , java.lang.Object is loaded first. Then class InitializationDemo is loaded, so InitializationDemo 's static initialization block is executed.

  2. InitializationDemo 's constructor is invoked (line 3), so InitializationDemo 's instance initialization block is executed.

  3. When executing new M() (line 7), class M needs to be loaded, which causes class M 's superclass (i.e., N ) to be loaded first. So N 's static initialization block is executed. (Note that N 's superclass java.lang.Object has already been loaded in (1)).

  4. Class M is now loaded. So M 's static initialization block is executed.

  5. When invoking M 's constructor, the no-arg constructor of M 's superclass is invoked first; therefore, N 's instance initialization block is executed.

  6. The regular code in N 's no-arg constructor is invoked after N 's instance initialization block is executed.


    [Page 330]
  7. After N 's no-arg constructor is invoked, M 's no-arg constructor is invoked, which causes M 's instance initialization block to be executed first.

  8. The regular code in M 's no-arg constructor is invoked after M 's instance initialization block is executed.

Note

If an instance variable is declared with an initial value (e.g., double radius = 5 ), the variable is initialized just as in an initialization block. That is, it is initialized when the constructor of the class is executed. If a static variable is declared with an initial value (e.g., static double radius = 5 ), the variable is initialized just as in a static initialization block. That is, it is initialized when the class is loaded.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net