Java static member initialization sequence

Source: Internet
Author: User

1. First static object, then non-static object, and then constructor.
2. After the static member is initialized, the object is initialized again. The static member is not initialized for the second time.

public class Bowl {      Bowl(int i){          System.out.println("Bowl"+i);      }      void Bowlf1(int i){          System.out.println("Bowl.f1--"+i);      }  }  
public class Table {      static Bowl bowl1=new Bowl(1);      Table(){          System.out.println("Table()");          bowl2.Bowlf1(1);      }      void Tablef2(int i){          System.out.println("Table.f2--"+i);      }      static Bowl bowl2=new Bowl(2);  }  
Public class cupboard {bowl bowl3 = new bowl (3); // initialize static bowl bowl4 = new bowl (4); // initialize the static object cupboard () {system. out. println ("cupboard ()"); bowl4.bowlf1 (2);} void cupboardf3 (int I) {system. out. println ("cupboard. f3 -- "+ I);} static bowl bowl5 = new bowl (5 );}
Public class staticinittest {public static void main (string [] ARGs) {system. out. println ("create cupboard in main"); new cupboard (); system. out. println ("==============================="); system. out. println ("create cupboard2 in main"); new cupboard (); system. out. println ("==============================="); table. tablef2 (1); system. out. println ("==============================="); cupboard. cupboardf3 (1);} static table = new table (); static cupboard = new cupboard ();}
Output:
Bowl1
Bowl2
Table ()
Bowl. F1 -- 1
Bowl4
Bowl5
Bowl3
Cupboard ()
Bowl. F1 -- 2
Create cupboard in main
Bowl3
Cupboard ()
Bowl. F1 -- 2
======================================
Create cupboard2 in main
Bowl3
Cupboard ()
Bowl. F1 -- 2
======================================
Table. F2 -- 1
======================================
Cupboard. F3 -- 1

Note:

The following two statements of code will be initialized first when the main function is executed to load the class,

static Table table = new Table();static Cupboard cupboard=new Cupboard();

Because: when the class is initialized as an object or a static member of the class may be accessed, the class is loaded.

If we do not initialize static members first, exceptions will occur when we use them below.

If you write Table = new table (); then this statement will not be executed. When will this statement be executed?

That is, when staticinittest is initialized,

For example, the main function of the test class writes staticinittest T = new staticinittest ();

Then Table = new table (); will be initialized at the same time!

See the following example:

Public class insect {private int I = 9; protected Int J; Public insect () {system. out. println ("I =" + I + ", j =" + J); j = 39;} Private Static int X1 = printinit ("static insect. x1 initialized "); static int printinit (string s) {system. out. println (s); Return 47 ;}} public class beetle extends insect {private int K = printinit ("beetle. k initialized "); Public beetle () {system. out. println ("k =" + k); system. out. println ("J =" + J);} Private Static int X2 = printinit ("static beetle. x2 initialized "); static insect = New Insect (); public static void main (string [] ARGs) {system. out. println ("beetle constructor"); beetle B = new beetle () ;}} output: static insect. x1 initialized static beetle. x2 initialized I = 9, j = 0 beetle constructor I = 9, j = 0 beetle. k initialized K = 47 J = 39

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.