Static code blocks for classes in Java, Construction code blocks, construction methods, initialization sequence of static members

Source: Internet
Author: User
Tags getcolor

First, describe

Static code blocks for classes in Java, Construction code blocks, constructor methods, initialization order of static members:

First , when the class is loaded, allocate the memory space for the static member variable in the class and give it a default value (the integer type is 0, the float type is 0.0, the Boolean is false, the character type is ' \u0000 ', the reference type is null);

Second , perform an explicit initialization of the static member variable. There are two methods of initializing static members, one for direct initialization at the time of Declaration, and the other for initializing with static blocks of code.

The two initialization methods are executed in the order in which they appear in the class, and only once when the class is loaded.

again , if an object of the class is created, the memory space is allocated for the instance of the class in the heap, and the instance variable is initialized to the default value (the default value is Xiangtong to the default value of the static member).

again, an explicit initialization of an instance variable is performed, which contains two initialization operations, one for direct initialization at the time of Declaration, and two for initialization in the construction code block, which are executed sequentially in the order in which they appear.

finally , the constructor of the class is constructed by invoking it once for each object you create and making multiple calls.

Summary: When a class is loaded, static members are initialized in the order in which static member variables or static code blocks appear (class loads are executed only once);

The normal member variable is then initialized in the order in which the normal member variable and the construction code block appear (several objects are created and executed several times);

Finally, the constructor of the class is called, and the member variables are initialized (several objects are created a few times).

Execution precedence: Static members and static code blocks > member variables and Construction code blocks > Construction Methods


Second, the source code

<span style= "FONT-SIZE:18PX;" >package tong.yue.day4_25;/** * Static code blocks for classes in Java, Construction code blocks, constructor methods, initialization order of static members: * First, when the class loads, allocate the memory space for the static member variable in the class and give it a default value (integer type 0, The floating-point type is 0.0, the Boolean is false, the character type is ' \u0000 ', the reference type is null, and second, the explicit initialization of the static member variable is performed. There are two methods of initializing static members, one for direct initialization at the time of Declaration, and the other for initializing with static blocks of code, which are executed in the order in which they appear in the class, and only once when the class is loaded. * Again, if an object of the class is created, the memory space is allocated to the instance of the class in the heap, and the instance variable is initialized to the default value (the default value is Xiangtong to the default value of the static member). * Again, perform an explicit initialization of the instance variable, which contains two initialization operations, one for direct initialization at the time of Declaration, and two for initialization in the construction code block, which are executed sequentially in the order in which they appear. * Finally, the constructor of the class is constructed by invoking it once for each object you create and making multiple calls. * Summary: When class is loaded, static members are initialized in the order in which static member variables or static code blocks appear (class loads are executed only once); * Then the normal member variables are initialized in the order in which the normal member variable and construction code block appear (several objects are created); * Finally, the constructor method of the class is called, Initializes the member variables (several objects are created and executed several times). * Execution Precedence: Static members and static code blocks > member variables and Construction code blocks > Construction Methods * * @author Tong * */public class Car {//parametric constructor 1public car (String color) {S Ystem.out.println ("Execution constructor car (String)"); this.color = color;} Constructs a block of code 1{SYSTEM.OUT.PRINTLN ("Execute initialization block 1"); Static initialization block 1static {System.out.println ("perform static initialization block 1"); length = 3.2;} The instance variable is directly initialized with private String color = GetColor ();//static member variable directly initialized with private static DOUBLE length = GetLength ();///Construct code block 2{SYSTEM.OUT.PRINTLN ("Execute initialization block 2");} Static initialization block 2static {System.out.println ("perform static initialization Block 2"); length = 3.2;} Static member variable initialization private static double width = getwidth ();//normal member variable initialization private int maxspeed = Getmaxspeed ();//parameterless constructor 2public Car () {System.out.println ("Execute constructor Car ()"); color = "Red";} Normal member method, get maxspeedprivate int getmaxspeed () {System.out.println ("Initialize member variable speed"); return 180;} Static member method, gets widthprivate static double getwidth () {System.out.println ("Initialize static member variable width"); return 2.0;} Static member method, gets lengthprivate static double GetLength () {System.out.println ("Initialize static member variable length"); return 3.2;} Normal member method, get Colorprivate String GetColor () {System.out.println ("Initialize instance variable color"); return color;}  public static void Main (string[] args) {new Car ();   Call the non-parametric construction method to create the object new Car ("Red"); Call a constructor method to create an object}}</span>

Execution Result:



Iii. Summary

1, static members and static code block two of the same order of occurrence determines the initialization sequence, their execution must precede the normal member variables initialization, construction code block initialization and constructor;

2, ordinary members and ordinary construction code blocks the order in which they appear in the code determines the order of initialization, and their execution must be followed by static members and static code blocks, but prior to the construction method;

3, compared with the above two, the constructor is always the last execution.

Static code blocks for classes in Java, Construction code blocks, construction methods, initialization sequence of static members

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.