Loading order of Java classes

Source: Internet
Author: User

loading Order of Java classes

The initialization rules for classes in Java are: Initialize the static member variable and the static block, initialize the non-static member variable and the non-static block, and finally initialize the constructor.
Example 1:

 PackageDemo/** * This class mainly introduces the loading order of Java Classes * * / Public  class testorder {     Public Static intK =0; Public StaticTestorder T1 =NewTestorder ("T1"); Public StaticTestorder t2 =NewTestorder ("T2"); Public Static inti = print ("I"); Public Static intn = About;Private intA =0; Public intj = Print ("J"); {Print ("Building Blocks"); }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 =NewTestorder ("Init"); }}

The result is:

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

Loading order of Java classes

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.