Introduction to Java class variable and member variable initialization process

  • 2020-04-01 01:43:03
  • OfStack

Class initialization

Class initialization: class initialization is generally only initialized once, class initialization is mainly the initialization of static member variables.

Class compilation determines the class initialization process.

The class file generated by the compiler mainly makes the following changes to the classes defined in the source file:

1)             Declare the member variables within the class in the order in which they are defined.

2)             The member variables are then initialized in the order in which they were initialized in the original Java class.

The corresponding transformation between a Java class and the compiled class is as follows:

The source file:


public class Person{
  public static String name=" Zhang SAN ";
  public static int age;
  static{
       age=20;
    System.out.println(" Initialize the age");
  }
  public static String address;
  static{
    address=" The Beijing municipal ";
    age=34;
  }
  public static void main(String[] args) {
                   System.out.println(name);
                   System.out.println(age);
                   System.out.println(address);
         }
}

When the Java source code is converted to a class file, it is converted to something like the following:

public class Person{
  public static String name;
  public static int age;
  public static String address;
  static{
    name=" Zhang SAN ";
    age=20;
    System.out.println(" Initialize the age");
    address=" The Beijing municipal ";
    age=34;
  }
  public static void main(String[] args) {
                   System.out.println(name);
                   System.out.println(age);
                   System.out.println(address);
         }
}

Initialization sequence according to the transformed the corresponding class class member variable initialization sequence of execution, so all of the static member variables are declared first, after the implementation of the assignment, and the order of the assignment is in accordance with the order of the source code to initialize static member variables, note: define a member variable and directly initialization and initialized in the static block of code are equivalent, is according to the order they are defined in the source code.


Two, object generation

The initialization process of object generation is similar to that of class initialization, but the constructor stage will be added. The source code is as follows:


public class Person{
   {
     name=" Li si ";
     age=56;
     System.out.println(" Initialize the age");
     address=" Shanghai ";
   }
   public String name=" Zhang SAN ";
   public int age=29;
   public String address=" The Beijing municipal ";
   public Person(){
     name=" Zhao six ";
     age=23;
     address=" Shanghai ";
   }
 }

After the compiler converts to the class file, it converts to something like this:

public class Person{
   public String name;
   public int age;
   public String address;
   public Person(){
     name=" Li si ";
     age=56;
     System.out.println(" Initialize the age");
     address=" Shanghai ";
     name=" Zhang SAN ";
     age=29;
     address=" The Beijing municipal ";
     name=" Zhao six ";
     age=23;
     address=" Shanghai ";
   }
 }

As you can see, the initialization of the member variables in the class and the code in the code block are all moved to the constructor, and the member variables are initialized in the Java source file initialization order, while the code in the original constructor is moved to the last execution of the constructor. I have been to the class initialization process has not been a profound understanding, is not sure exactly how initialization, can only remember the initialization sequence according to the book, but over a period of time has forgotten, so at this time to figure out, or according to a model to explain the initialization mechanism is good, need not back again, only to understand the difficult to forget


Related articles: