Application of Java class variables and member variable initialization process _java

Source: Internet
Author: User

Initialization of a class

Initialization of a class: The initialization of a class is typically initialized only once, and the initialization of the class is primarily the initialization of static member variables.

The compilation of a class determines the initialization of the class.

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

1 Declare the member variables within the class by the definition order of the static member variables.

2) and initialize the initialization order of the member variables in the original Java class.

A Java class and a compiled class correspond to the following transformations:

Source file:

Copy Code code as follows:

public class person{
public static String Name= "John";
public static int age;
static{
age=20;
SYSTEM.OUT.PRINTLN ("Initialization Age");
}
public static String address;
static{
address= "Beijing";
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 into a class file, it is converted to code similar to the following:
Copy Code code as follows:

public class person{
public static String name;
public static int age;
public static String address;
static{
Name= "John";
age=20;
SYSTEM.OUT.PRINTLN ("Initialization Age");
address= "Beijing";
age=34;
}
public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN (name);
System.out.println (age);
SYSTEM.OUT.PRINTLN (address);
}
}

The initialization order is executed sequentially according to the initialization order of the class member variables corresponding to the transformation, so all static member variables are declared first, then assigned, and the order of assignment is also in the order of initialization of the static member variables in the source code. Note: Defining a member variable and initializing it directly is equivalent to initializing in a static block of code, based on the order in which they are defined in the source code.


Second, the generation of objects

The initialization of an object is similar to the initialization of the class, but increases the constructor phase with the source code as follows:

Copy Code code as follows:

public class person{
{
Name= "Dick";
age=56;
SYSTEM.OUT.PRINTLN ("Initialization Age");
Address= "Shanghai";
}
Public String name= "John";
public int age=29;
Public String address= "Beijing";
Public person () {
Name= "Zhao Liu";
age=23;
address= "Shanghai City";
}
}

when the compiler converts to a class file, it is converted to code similar to the following:
Copy Code code as follows:

public class person{
public String name;
public int age;
public String address;
Public person () {
Name= "Dick";
age=56;
SYSTEM.OUT.PRINTLN ("Initialization Age");
Address= "Shanghai";
Name= "John";
age=29;
address= "Beijing";
Name= "Zhao Liu";
age=23;
address= "Shanghai City";
}
}

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. The member variables are initialized in the order of initialization of the Java source files, and 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 clear how it is initialized, only according to the book to remember the initialization order, but some time to forget, so this time to make it clear, or according to a model to explain the initialization mechanism better AH, no need to back up, It's hard to forget if you understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.