JAVA class loading sequence

Source: Internet
Author: User

JAVA class loading sequence
JAVA class loading sequence

Class initialization rules in JAVA are as follows: Initialize static member variables and static blocks first, then initialize non-static member variables and non-static blocks, and finally initialize constructors.
Example 1:

Package demo;/*** this class mainly describes the loading sequence of java classes **/public class TestOrder {public static int k = 0; public static TestOrder t1 = new TestOrder ("t1"); public static TestOrder t2 = new TestOrder ("t2"); public static int I = print ("I "); public static int n = 99; private int a = 0; public int j = print ("j"); {print (" ");} static {print ("static block");} public TestOrder (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 args []) {TestOrder t = new TestOrder ("init ");}}

The result is:

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

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.