Java class construction order, Java class construction order

Source: Internet
Author: User

Java class construction order, Java class construction order
1. No Object Construction

 

Public class Test {public static int k = 0; public static int n = 99; public static int I = print ("I "); static {print ("static block") ;}{ print ("") ;}public int j = print ("j"); public Test (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ I; ++ n ;} public static int print (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ n; return ++ I ;} public static void main (String [] strings ){}}

Construct static variables and static code blocks when loading classes.

The order is defined.

 

Result:

1: I = 0 n = 99
2: static block I = 1 n = 100

 

 

 

2. Construct an object in the main function

 

Public class Test {public static int k = 0; public static int n = 99; public static int I = print ("I "); static {print ("static block") ;}{ print ("") ;}public int j = print ("j"); public Test (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ I; ++ n ;} public static int print (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ n; return ++ I ;} public static void main (String [] strings) {Test t = new Test ("init ");}}

The first two are the same as above.

When constructing an object, construct non-static variables in the object before calling the constructor.

Non-static class variables are constructed in the defined order.

 

Result:

1: I = 0 n = 99
2: static block I = 1 n = 100
3: Construct block I = 2 n = 101
4: j I = 3 n = 102
5: init I = 4 n = 103.

 

 

3. The class contains static objects.

 

Public class Test {public static int k = 0; public static int n = 99; public static Test t1 = new Test ("t1 "); public static int I = print ("I"); static {print ("static block") ;}{ print ("constructed block ");} public int j = print ("j"); public static Test t2 = new Test ("t2"); public Test (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ I; ++ n ;} public static int print (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ n; return ++ I ;} public static void main (String [] strings) {Test t = new Test ("init ");}}


 

Static variables, objects, and code blocks are constructed in the defined order.

When constructing a static object, as the object is constructed, the non-static variable is first constructed as described in section 2, and the constructor is called.

 

Result:

1: Construct block I = 0 n = 99
2: j I = 1 n = 100
3: t1 I = 2 n = 101
4: I = 3 n = 102
5: static block I = 4 n = 103
6: Construct block I = 5 n = 104
7: j I = 6 n = 105
8: t2 I = 7n = 106
9: Construct block I = 8 n = 107
10: j I = 9 n = 108
11: init I = 10 n = 109

 

 

4. static variable Initialization

 

Public class Test {public static int k = 0; public static Test t1 = new Test ("t1"); public static int I = print ("I "); public static int n = 99; static {print ("static block") ;}{ print ("");} public int j = print ("j "); public static Test t2 = new Test ("t2"); public Test (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ I; ++ n ;} public static int print (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ n; return ++ I ;} public static void main (String [] strings) {Test t = new Test ("init ");}}


 




Initialize by default before assigning values.

 

Result:

1: Construct block I = 0 n = 0
2: j I = 1 n = 1
3: t1 I = 2 n = 2
4: I = 3 n = 3
5: static block I = 4 n = 99
6: Construct block I = 5 n = 100
7: j I = 6 n = 101
8: t2 I = 7n = 102
9: Construct block I = 8 n = 103
10: j I = 9 n = 104
11: init I = 10 n = 105

 

 

 


Java constructor Sequence

If A Class A inherits another class B. Then, when executing the constructor (which is not the start of the supper () Statement in the constructor) of Class A, spuer () will be executed first, that is, Chile (String name) {} constructor. during compilation, it will become public Child (String name ){
Super ();
System. out. print (3 );
This. name = name;
Father = new People (name + ": F ");}
Similarly, we don't need to explain the following estimation. The same principle. Super () indicates the no-argument constructor of the parent class of this class.

What is the initialization sequence of classes in Java?

The execution sequence should be: the first is the static block of the parent class --> then the static block of the subclass --> the free block of the parent class --> the constructor block of the parent class --> the free block of the subclass --> the constructor block of the subclass class.

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.