The construction order of Java classes

Source: Internet
Author: User

1. No object construction

public class Test {public      static int k = 0;     public static int n =;           public static int i = print ("i"); static {           print ("static block");       } {          print ("Building block");       } 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) {              }   }

Constructs static variables and static blocks of code when the class is loaded.

Order according to the defined order.

The result is:

1:i i=0 n=99
2: Static block I=1 n=100

Constructing objects in 2.main functions

public class Test {public      static int k = 0;     public static int n =;           public static int i = print ("i"); static {           print ("static block");       } {          print ("Building block");       } 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 ibid.

When constructing an object, the non-static variable in the object is constructed before the constructor is called.

Variables of non-static classes are constructed in the order defined.

The result is:

1:i i=0 n=99
2: Static block I=1 n=100
3: Construction block i=2 n=101
4:j i=3 n=102
5:init i=4 n=103

3. The class contains a static object

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 ("Building 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 type variables, objects, and code blocks are constructed in the order defined.

When constructing a static object, because it is a construction object, as described in 2, a non-static variable is constructed before the constructor is called.

Results:

1: Construction block i=0 n=99
2:j I=1 n=100
3:t1 i=2 n=101
4:i i=3 n=102
5: Static block i=4 n=103
6: Construction block i=5 n=104
7:j i=6 n=105
8:t2 i=7 n=106
9: Construction 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 ("Building 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");       }   }





The default initialization is then assigned.

Results:

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

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.