Java class variables and member variables initialization process, java Variables

Source: Internet
Author: User

Java class variables and member variables initialization process, java Variables

I. class initialization

Class initialization: class initialization is generally only performed once, and class initialization is mainly used to initialize static member variables.

Class compilation determines the class initialization process.

The class file generated by the compiler mainly changes the class defined in the source file as follows:

The conversion between a java class and the compiled class is as follows:

Source file:

Public class Person {
Public static String name = "zhangsan ";
Public static int age;
Static {
Age = 20;
System. out. println ("initialize 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 );
}
}

After the java source code is converted into a class file, it is converted into code similar to the following:

Public class Person {
Public static String name;
Public static int age;
Public static String address;
Static {
Name = "James ";
Age = 20;
System. out. println ("initialize 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 sequence is executed in sequence based on the initialization sequence of the corresponding class member variables after the conversion. Therefore, all static member variables are declared first and then assigned values, in addition, the assignment order is also carried out according to the source code initialization sequence of static member variables. Note: defining a member variable and directly initializing is equivalent to initializing in a static code block, they are all based on the order they are defined in the source code.

 

Ii. Object generation

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

1 public class Person {
2 {
3 name = "Li Si ";
4 age = 56;
5 System. out. println ("initialize age ");
6 address = "Shanghai ";
7}
8 public String name = "zhangsan ";
9 public int age = 29;
10 public String address = "Beijing ";
11 public Person (){
12 name = "Zhao Liu ";
13 age = 23;
14 address = "Shanghai City ";
15}
16}

After the compiler is converted to a class file, it is converted to code similar to the following:

1 public class Person {
2 public String name;
3 public int age;
4 public String address;
5 public Person (){
6 name = "Li Si ";
7 age = 56;
8 System. out. println ("initialize age ");
9 address = "Shanghai ";
10 name = "James ";
11 age = 29;
12 address = "Beijing ";
13 name = "Zhao Liu ";
14 age = 23;
15 address = "Shanghai City ";
16}
17}

We can see that 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 sequence according to the initialization sequence of the java source files, the code in the original constructor is moved to the final execution of the constructor.

Reprinted from Imtoo

Related Article

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.