Java object initialization sequence (1)

Source: Internet
Author: User

Java object initialization sequence (1)
1. copy the code public class Test2 {public int Field; // step 1 public Test2 () {Field = 1 ;}} /*************************************** * ********* // The purpose of the class is only to verify the sequence of execution of the parent class subclass constructor */public class Test3 extends Test2 {// step 2 public Test3 () {Field = 2;} public static void main (String [] args) {Test3 t3 = new Test3 (); System. out. println ("the field value in object t3 is [" + t3.Field + "]"); /************************* * ********* \ * Output result * The Field Value in object t3 is [2 ]\************* * **********************/} copy the code conclusion: instantiate an object. The execution sequence of the construction is from the parent class to the subclass, that is, Test2-> Test3 2. constructor initialization and inline mode initialization copy code/*** this class is only intended to verify the incomposite mode of initialization of fields and the order of * fields initialized in the constructor ** inline Initialization inline meaning, it is to initialize and assign values when declaring fields. */Public class Test1 {public int Field = 1; // step 1/*** step 2 */public Test1 () {Field = 2 ;} public static void main (String [] args) {Test1 t1 = new Test1 (); System. out. println ("the field value in object t1 is [" + t1.Field + "]"); /*********************************** \ * output result * The Field Value in object t1 is [2 ]\*************************** * ********/} copy the code conclusion: to instantiate an object, you will first execute the code of the inline initialization field, and then execute the content in the constructor 3. static block and inline mode (static) initialize and copy the code public class Test4 {public static int Field = 1; // step 1/*** step 2 */static {Field = 2;} public static void main (String [] args) {System. out. println ("the Field value is [" + Field + "]"); /*********************************** \ * output result * The Field value is [2 ]\***************************** * ******/} copy the code conclusion: initialize static fields in inline mode before initializing fields in static blocks. initialize block and initialize block (static) Copy code public class Test5 {public static int Field; // initialize block (static) static {Field = 1 ;} // initialize the block {Field = 2;} public static void main (String [] args) {System. out. println ("the Field value is [" + Field + "]"); /*************************************\* output result * The Field value is [1 ]\**************************** * ********/} copy the code conclusion: the initialization block takes precedence over the execution of the static initialization block (incorrect conclusion). This conclusion is strange and I checked some information, it is learned that the initialization block will only execute the copy code public class Test5 {public static int Field; // The initialization block (static) static {Field = 1 ;} // initialize the block {Field = 2;} public static void main (String [] args) {System. out. println ("the Field value is [" + Field + "]"); Test5 test5 = new Test5 (); System. out. println ("after the object is instantiated, the Field value is [" + Field + "]"); // to verify that each instantiated object will call the initializer Field = 3; System. out. println ("assign a value to the Field. The Field value is [" + Field + "]"); Test5 test5_0 = new Test5 (); System. out. println ("after the object is instantiated again, the Field value is [" + Field + "]"); /*************************************\* before the object is instantiated, the Field value is [1] * after the object is instantiated, the Field value is [2] * assign a value to the Field, and the Field value is [3] * after the object is instantiated again, the Field value is [2 ]\******************************** * *****/} copy the code to verify the correctness of the red font mentioned above, the Field value is also assigned to 3 and the object is instantiated again. Conclusion: The initialized object will be executed only when it is instantiated, and will be executed every time it is instantiated, the static initialization tool is executed before the initialization tool. 5. copy the public class Test6 {public static int I = Test7.j; {I ++;} static {I = 0 ;}public Test6 () {I ++ ;}} /*************************************** * *****/public class Test7 extends Test6 {public static int j = Test6. I; static {j ++ ;}{ j ++ ;}} /*************************************** * ***/public class Main {public static void main (String [] args) {Test7 t7 = new Test7 (); System. out. println ("the value of field I is [" + Test6. I + "]"); System. out. println ("the value of field J is [" + Test7.j + "]") ;}} copy Code 6. copy the public class Test6 {public static int I = Test7.j; {I ++;} static {I = 0 ;}public Test6 () {I ++ ;}} /*************************************** * *****/public class Test7 {public static int j = Test6. I; static {j ++ ;}{ j ++ ;}} /*************************************** * *****/public class Main {public static void main (String [] args) {Test7 t7 = new Test7 (); System. out. println ("the value of field I is [" + Test6. I + "]"); System. out. println ("the value of field J is [" + Test7.j + "]");}}

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.