Initialization block and execution of Java Comments

Problem: Java object initialization main methods are there? What are they?
For the above problem, surely we the first to come to mind is the answer to the constructor, yes, the constructor is commonly used Java object initialization method.

There is also a builder effect and is very similar to the initialization block, which can be initialized to operate Java objects. The following describes the main block and the initialization execution of Java.

Java initialization block is actually a member of the Java class, its syntax is as follows:

[Modifier] {

  // initialization block of executable code

  ...

}

Modifier initialization block can only be static, the static modifier is called static initialization block initialization block, it will be introduced to the back.

What happens when you create an object Dog Let's look through some code.

 

public  class Dog { 

    // definition of an initialization block 
    { 
        System.out.println ( "first initialization block" ); 
    } 
    // define a second initialization block 
    { 
        System.out.println ( "second initialization block" ) ; 
    } 
    // definition of non-argument constructor 
    public Dog () { 
        System.out.println ( "Dog no arguments constructor" ); 
    } 
    
    public  static  void main (String [] args) { 
        
        new new Dog (); 
    } 

}

Operating results as follows:

Initializing a first block of 
the second initialization block 
Dog no arguments constructor

As can be seen from the results: When creating a Dog object, the program execution to the initialization block constructor, and the order of execution of two initialization is performed in accordance with the order of the front and rear.

Since the initialization block execution hermit only when creating Java objects (all initialization block all execution), in order to make the program more concise and more readable, usually a maximum of only one class initialization block.

Since the initialization block and constructors are created when an object is executed, then they have what difference does it make?
To some degree, initialization block complement constructor, and the constructor is different, a fixed initialization block of code is executed, can not receive any parameters, and may receive a constructor parameter. If the program has two constructors, and they have common without receiving the code parameters, these can be put into the same common code to initialize the block, more maintainable code.

When the normal initialization block is modified static, then the initialization block is static initialization block, also called the class initialization block.

General responsible for the initialization block to initialize the object, while the static initialization block is responsible for the class is initialized, the static initialization block is always executed first than normal initialization block.

Static blocks are typically used to perform the initialization processing to initialize the class variables, instance variables can not perform initialization processing.

Static initialization block is a static class members, static members still need to follow the rules can not access non-static members, including the inability to access the instance variables and instance methods.

When initialization block execution would have been traced back to the java.lang.Object class, perform initialization block Object, and then execute the parent class initialization block ... finally perform their own class initialization block.

The result of this execution order look following code:

class Biology {
     static { 
        System.out.println ( "static initialization block Biology" ); 
    } 
    { 
        System.out.println ( "normal initialization block Biology" ); 
    } 
    public Biology () { 
        System.out.println ( " Biology, no arguments constructor " ); 
    } 
} 

class Animal the extends Biology {
     static { 
        System.out.println ( " Animal static initialization block " ); 
    } 
    { 
        System.out.println ( " normal initialization block of Animal " ); 
    } 
    public Animal () {
        System.out.println ( "Animal no arguments constructor" ); 
    } 
    public Animal (String name) {
         the this (); // call the constructor overloads in the same 
        System.out.println ( "reference configuration with the Animal device, name = "+ name); 
    } 
} 

class Cat the extends Animal {
     static { 
        System.out.println ( " Cat static initialization block " ); 
    } 
    { 
        System.out.println ( " Cat normal initialization block " ); 
    } 
    public Cat () {
         // call the parent class's constructor arguments with 
        Super ( "the Java books" );
        System.out.println ( "Cat no arguments constructor" ); 
    } 
} 

public  class the Test { 

    public  static  void main (String [] args) {
         new new Cat (); 
        System.out.println ( "================ ==== " );
         new new Cat (); 
    } 

}

Execution results are as follows:

Static initialization block Biology of 
Animal static initialization block 
static initialization block Cat's 
normal initialization block Biology of 
no arguments constructor Biology of 
Animal common initialization block 
Animal no arguments constructor 
of Animal parameterized constructor, name = the Java books 
of Cat Common initialization block 
Cat no arguments constructor
 ========= 
normal initialization block Biology of 
Biology, no arguments constructor 
normal initialization block of Animal 
no arguments constructor of Animal 
Animal a parameterized constructor, name = the Java Classics 
common initialization block of Cat 
Cat no arguments constructor

From the results of the above point of view, when you first create Cat objects, because the system has not yet Cat object, so you need to load and initialize the Cat class, initialize the Cat class will first perform a static initialization block its top-level parent, and then execute static initialization block its direct parent class, and finally perform the static initialization block itself.

Once the Cat class initialization is successful, it has always existed in a virtual machine, when the second and then create Cat Cat class objects no longer need to be initialized.

In summary, the order of execution of a class is initialized: static initialization block parent class, the class static initialization block, initialization block common parent, the parent class constructors, class initialization block general, the class constructor.

 

Welcome concern public micro-channel number] [Java books, Java technology watch more dry! Java concern Jisong a full set of data

   ▼ sweep the next micro-channel two-dimensional code Follow FIG ↓↓↓

 

 

Guess you like

Origin www.cnblogs.com/bingyimeiling/p/11529403.html